function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
michelemcgeoy.ax1914michelemcgeoy.ax1914 

error: Invalid type - in Test Class

I'm getting the error "Invalid type: CloneClassAttendance" in test code bolded below.
We're supposed to go live tomorrow morning and I'm stuck... don't mean to sound too desperate... but I am :(
Thanks, Michele

Here's the test code:
@isTest
private class CloneClassAttendanceTest {
//CloneClassAttendance myTest = new CloneClassAttendance();
//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.CloneClassAttendance;
         //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
         //Line below gives error: Invalid type: CloneClassAttendance       
         CloneClassAttendance cca = new CloneClassAttendance(s);

         //Call your extensions methods here...
         cca.InsertRecord();
    }
}

Here's the Apex code I'm trying to test:
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's the Visual Force 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>
willardwillard
I'm stumped on this one.  All your test code runs in the VF page correctly when testing manually?  The only thing I can think of is that the CCA class never compiled, or the name was changed.  Maybe try re-saving the CCA class and VF page before trying to save the test class?
olegforceolegforce
What is your page name? 
michelemcgeoy.ax1914michelemcgeoy.ax1914
The page name is also CloneClassAttendance.
michelemcgeoy.ax1914michelemcgeoy.ax1914
I had been working in Force.com IDE when I got the error. When I copy/paste the test code into the Developers Console and run it there it shows "Success" but then nothing in code coverage. I save the test code but when I look in SF->Admin->Apex it's still showing the old code. When I save code in the developers console isn't it supposed to write it back to salesforce?
thanks, Michele
olegforceolegforce
It should... 

Couple of things - salesforce is really picky how and where you work with your code - so stick with one editor. Always refresh from server before you start working on the code. The other thing - apex is a script language, it gets interpreted by java in the back end at a run time. This means that while it lets you save things like Id id = 'id' and Account contact = new Account (), it is not recommended to do so and confuses the heck out of people looking through your code :) Try using meaningful variables like adding word Controller or Extension to your page controllers and giving different names to pages and classes.  

Let's try to rename your class to some other name than your page and try it again. 
michelemcgeoy.ax1914michelemcgeoy.ax1914
I stopped working with the other editors and just went back into the sandbox and did the editing there. The test seemed to run there. Though now I'm only getting 56% coverage.
I commented out the DayWeek method and change the test code to initialize the dates to be a month, but that didn't seem to make any difference.
Is there a way to see what code is not actually getting run?
 When I look through the code it seems like it should run from top to bottom. Not sure what I'm missing.
Here's what I'm running now:
@isTest
private class CloneClassAttendanceTest {
//CloneClassAttendance myTest = new CloneClassAttendance();
//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', clonestart__c = date.today(), cloneend__c = date.today()+30);
        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.CloneClassAttendance;
         //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
         //Line below gives error: Invalid type: CloneClassAttendance       
         CloneClassAttendance cca = new CloneClassAttendance(s);
         //Call your extensions methods here...
         cca.InsertRecord();
    }
}
michelemcgeoy.ax1914michelemcgeoy.ax1914
When I Deployed the code along with the VF page it worked.
Thanks for your help!