You need to sign in to do that
Don't have an account?
michelemcgeoy.ax1914
Help with Test class
I've got code that works in Developer mode and now need to bring it into production... of course we're supposed to go live tomorrow...
I'm very new to Apex so please excuse the basic question. My code gets called from a visual force page and when I run the test code it can't seem to find the method. I get the error: Method does not exist or incorrect signature
Here is the test code:
@isTest
private class CloneClassAttendanceTest {
//Public CloneClassAttendance(){}
// above line gives error Invalid constructor name:
static testMethod void myUnitTest() {
// Add test contact
Contact c = new Contact(LastName='Test');
insert c;
// Add test campaign
Campaign cp = new Campaign(Name='TestCampaign');
insert cp;
// Add campaign member status
CampaignMemberStatus cms = new CampaignMemberStatus( CampaignId = cp.Id, Label = 'Attended', SortOrder = 3);
insert cms;
// Add campaign member
CampaignMember cm = new CampaignMember( CampaignId = cp.Id, Status = 'Attended', ContactId = c.Id);
insert cm;
// Call CloneClassAttendance
InsertRecord();
// Above line gives error: Method does not exist or incorrect signature
}
}
Here is that Apex Class:
public class CloneClassAttendance
{
private final Campaign theCampaign;
public CloneClassAttendance( ApexPages.StandardController stdController )
{
theCampaign = (Campaign)stdController.getRecord();
}
// returns the day-of-week for the given date in 2001 - 2099
// this works because 2001 started on a Monday
// 0 = Sunday, ... 6 = Saturday
private static Integer dayOfWeek( Date theDate )
{
Integer theYear = theDate.year() - 2001;
return Math.mod( theYear + theYear/4 + theDate.dayOfYear(), 7 );
}
public void insertRecord()
{
Map<Date,Campaign> map_newCampaigns = new Map<Date,Campaign>();
Date newDate = theCampaign.CloneStart__c;
while ( newDate <= theCampaign.CloneEnd__c )
{
map_newCampaigns.put
( newDate,
new Campaign
( Type = 'Class Attendance',
IsActive = true,
StartDate = newDate,
EndDate = newDate,
ParentId = theCampaign.Id,
Name = theCampaign.Name + '-' + newDate.format(),
Description = theCampaign.Description,
Teacher__c = theCampaign.Teacher__c,
Day_of_Week__c = theCampaign.Day_of_Week__c,
Hours_Per_Class__c = theCampaign.Hours_Per_Class__c,
Location__c = theCampaign.Location__c,
Start_Time__c = theCampaign.Start_Time__c,
End_Time__c = theCampaign.End_Time__c,
Waitlist__c = theCampaign.Waitlist__c,
Max_Capacity__c = theCampaign.Max_Capacity__c,
Status = 'Completed'
)
);
newDate = newDate.addDays( 7 );
}
insert map_newCampaigns.values();
List<CampaignMemberStatus> list_newCampaignMemberStatus = new List<CampaignMemberStatus>();
List<CampaignMember> list_newCampaignMembers = new List<CampaignMember>();
newDate = theCampaign.CloneStart__c;
while ( newDate <= theCampaign.CloneEnd__c )
{
Campaign newCampaign = map_newCampaigns.get( newDate );
for ( CampaignMemberStatus status :
[ SELECT Id, HasResponded, IsDefault, Label, SortOrder
FROM CampaignMemberStatus
WHERE ( CampaignId = :theCampaign.Id
AND IsDeleted = false
)
]
)
{
list_newCampaignMemberStatus.add
( new CampaignMemberStatus
( CampaignId = newCampaign.Id,
HasResponded = status.HasResponded,
IsDefault = status.IsDefault,
Label = status.Label,
// the following line is a workaround due to the fact that when the campaign is cloned it automatically
// add CampaignMemberStatus of Sent and Received with sortorder 1 and 2. This was creating a duplicate bug.
SortOrder = status.SortOrder +2
)
);
System.debug( 'CampaignMemberStatus: CampaignId= ' + newCampaign.Id +' Label = ' + status.Label + ', SortOrder = ' + status.SortOrder );
}
for ( CampaignMember member :
[ SELECT Id, ContactId, Status
FROM CampaignMember
WHERE CampaignId = :theCampaign.Id
]
)
{
list_newCampaignMembers.add
( new CampaignMember
( CampaignId = newCampaign.Id,
ContactId = member.ContactId,
Status = member.Status
)
);
}
newDate = newDate.addDays( 7 );
}
insert list_newCampaignMemberStatus;
insert list_newCampaignMembers;
}
}
Here is the VisualForce Page:
<apex:page standardController="Campaign" extensions="CloneClassAttendance" action="{!insertRecord}">
<h1>Cloning Attendance</h1>
<p> Class name is {! campaign.name} </p>
<p> Class id is: {! campaign.id} </p>
<p> Class start date is: {! campaign.startdate} </p>
<p> Class end date is: {! campaign.enddate} </p>
<p> Next week is: {! campaign.startdate+7} </p>
<p> Class Status is {! campaign.status} </p>
<p> Class teacher is {! campaign.teacher__c} </p>
<p> Class DOW is {! campaign.Day_of_Week__c} </p>
<p> Class Hours_Per_Class__c is {! campaign.Hours_Per_Class__c} </p>
<p> Class Location is {! campaign.Location__c} </p>
<p> Class Start time is {! campaign.Start_Time__c} </p>
<p> Class End time is {! campaign.End_Time__c} </p>
<p> Class Description is {! campaign.Description} </p>
<p> Class Waitlist is {! campaign.Waitlist__c} </p>
<p> Class Max_Capacity is {! campaign.Max_Capacity__c} </p>
<p> Clone start date is: {! campaign.clonestart__c} </p>
<p> Class end date is: {! campaign.cloneend__c} </p>
</apex:page>
I'm very new to Apex so please excuse the basic question. My code gets called from a visual force page and when I run the test code it can't seem to find the method. I get the error: Method does not exist or incorrect signature
Here is the test code:
@isTest
private class CloneClassAttendanceTest {
//Public CloneClassAttendance(){}
// above line gives error Invalid constructor name:
static testMethod void myUnitTest() {
// Add test contact
Contact c = new Contact(LastName='Test');
insert c;
// Add test campaign
Campaign cp = new Campaign(Name='TestCampaign');
insert cp;
// Add campaign member status
CampaignMemberStatus cms = new CampaignMemberStatus( CampaignId = cp.Id, Label = 'Attended', SortOrder = 3);
insert cms;
// Add campaign member
CampaignMember cm = new CampaignMember( CampaignId = cp.Id, Status = 'Attended', ContactId = c.Id);
insert cm;
// Call CloneClassAttendance
InsertRecord();
// Above line gives error: Method does not exist or incorrect signature
}
}
Here is that Apex Class:
public class CloneClassAttendance
{
private final Campaign theCampaign;
public CloneClassAttendance( ApexPages.StandardController stdController )
{
theCampaign = (Campaign)stdController.getRecord();
}
// returns the day-of-week for the given date in 2001 - 2099
// this works because 2001 started on a Monday
// 0 = Sunday, ... 6 = Saturday
private static Integer dayOfWeek( Date theDate )
{
Integer theYear = theDate.year() - 2001;
return Math.mod( theYear + theYear/4 + theDate.dayOfYear(), 7 );
}
public void insertRecord()
{
Map<Date,Campaign> map_newCampaigns = new Map<Date,Campaign>();
Date newDate = theCampaign.CloneStart__c;
while ( newDate <= theCampaign.CloneEnd__c )
{
map_newCampaigns.put
( newDate,
new Campaign
( Type = 'Class Attendance',
IsActive = true,
StartDate = newDate,
EndDate = newDate,
ParentId = theCampaign.Id,
Name = theCampaign.Name + '-' + newDate.format(),
Description = theCampaign.Description,
Teacher__c = theCampaign.Teacher__c,
Day_of_Week__c = theCampaign.Day_of_Week__c,
Hours_Per_Class__c = theCampaign.Hours_Per_Class__c,
Location__c = theCampaign.Location__c,
Start_Time__c = theCampaign.Start_Time__c,
End_Time__c = theCampaign.End_Time__c,
Waitlist__c = theCampaign.Waitlist__c,
Max_Capacity__c = theCampaign.Max_Capacity__c,
Status = 'Completed'
)
);
newDate = newDate.addDays( 7 );
}
insert map_newCampaigns.values();
List<CampaignMemberStatus> list_newCampaignMemberStatus = new List<CampaignMemberStatus>();
List<CampaignMember> list_newCampaignMembers = new List<CampaignMember>();
newDate = theCampaign.CloneStart__c;
while ( newDate <= theCampaign.CloneEnd__c )
{
Campaign newCampaign = map_newCampaigns.get( newDate );
for ( CampaignMemberStatus status :
[ SELECT Id, HasResponded, IsDefault, Label, SortOrder
FROM CampaignMemberStatus
WHERE ( CampaignId = :theCampaign.Id
AND IsDeleted = false
)
]
)
{
list_newCampaignMemberStatus.add
( new CampaignMemberStatus
( CampaignId = newCampaign.Id,
HasResponded = status.HasResponded,
IsDefault = status.IsDefault,
Label = status.Label,
// the following line is a workaround due to the fact that when the campaign is cloned it automatically
// add CampaignMemberStatus of Sent and Received with sortorder 1 and 2. This was creating a duplicate bug.
SortOrder = status.SortOrder +2
)
);
System.debug( 'CampaignMemberStatus: CampaignId= ' + newCampaign.Id +' Label = ' + status.Label + ', SortOrder = ' + status.SortOrder );
}
for ( CampaignMember member :
[ SELECT Id, ContactId, Status
FROM CampaignMember
WHERE CampaignId = :theCampaign.Id
]
)
{
list_newCampaignMembers.add
( new CampaignMember
( CampaignId = newCampaign.Id,
ContactId = member.ContactId,
Status = member.Status
)
);
}
newDate = newDate.addDays( 7 );
}
insert list_newCampaignMemberStatus;
insert list_newCampaignMembers;
}
}
Here is the VisualForce Page:
<apex:page standardController="Campaign" extensions="CloneClassAttendance" action="{!insertRecord}">
<h1>Cloning Attendance</h1>
<p> Class name is {! campaign.name} </p>
<p> Class id is: {! campaign.id} </p>
<p> Class start date is: {! campaign.startdate} </p>
<p> Class end date is: {! campaign.enddate} </p>
<p> Next week is: {! campaign.startdate+7} </p>
<p> Class Status is {! campaign.status} </p>
<p> Class teacher is {! campaign.teacher__c} </p>
<p> Class DOW is {! campaign.Day_of_Week__c} </p>
<p> Class Hours_Per_Class__c is {! campaign.Hours_Per_Class__c} </p>
<p> Class Location is {! campaign.Location__c} </p>
<p> Class Start time is {! campaign.Start_Time__c} </p>
<p> Class End time is {! campaign.End_Time__c} </p>
<p> Class Description is {! campaign.Description} </p>
<p> Class Waitlist is {! campaign.Waitlist__c} </p>
<p> Class Max_Capacity is {! campaign.Max_Capacity__c} </p>
<p> Clone start date is: {! campaign.clonestart__c} </p>
<p> Class end date is: {! campaign.cloneend__c} </p>
</apex:page>
@isTest
private class CloneClassAttendanceTest {
//
Public CloneClassAttendance(){}// above line gives error Invalid constructor name:
static testMethod void myUnitTest() {
// Add test contact
Contact c = new Contact(LastName='Test');
insert c;
// Add test campaign
Campaign cp = new Campaign(Name='TestCampaign');
insert cp;
// Add campaign member status
CampaignMemberStatus cms = new CampaignMemberStatus( CampaignId = cp.Id, Label = 'Attended', SortOrder = 3);
insert cms;
// Add campaign member
CampaignMember cm = new CampaignMember( CampaignId = cp.Id, Status = 'Attended', ContactId = c.Id);
insert cm;
//Page reference change VF_PageName with the name of your visualforce page
PageReference cloneAttendance = Page.VF_PageName;
//Set the current page
Test.setCurrentPage(cloneAttendance);
//Set the id param on the page to the campaign id
ApexPages.currentPage().getParameters().put('id', cp.Id);
//Instantiate standard controller with the campaign record
ApexPages.StandardController s = new APexPages.StandardController(cp);
//Instantiate extension class passing the standard controller
CloneClassAttendance cca = new CloneClassAttendance(s);
//Call your extensions methods here...
cca.InsertRecord();
}
}
All Answers
@isTest
private class CloneClassAttendanceTest {
//
Public CloneClassAttendance(){}// above line gives error Invalid constructor name:
static testMethod void myUnitTest() {
// Add test contact
Contact c = new Contact(LastName='Test');
insert c;
// Add test campaign
Campaign cp = new Campaign(Name='TestCampaign');
insert cp;
// Add campaign member status
CampaignMemberStatus cms = new CampaignMemberStatus( CampaignId = cp.Id, Label = 'Attended', SortOrder = 3);
insert cms;
// Add campaign member
CampaignMember cm = new CampaignMember( CampaignId = cp.Id, Status = 'Attended', ContactId = c.Id);
insert cm;
//Page reference change VF_PageName with the name of your visualforce page
PageReference cloneAttendance = Page.VF_PageName;
//Set the current page
Test.setCurrentPage(cloneAttendance);
//Set the id param on the page to the campaign id
ApexPages.currentPage().getParameters().put('id', cp.Id);
//Instantiate standard controller with the campaign record
ApexPages.StandardController s = new APexPages.StandardController(cp);
//Instantiate extension class passing the standard controller
CloneClassAttendance cca = new CloneClassAttendance(s);
//Call your extensions methods here...
cca.InsertRecord();
}
}
Now I get the error: Invalid type: CloneClassAttendance
on the line:
CloneClassAttendance cca = new CloneClassAttendance(s);
Any ideas?
Thanks, Michele
Thanks for your help!!!