+ Start a Discussion
kshannonkshannon 

Need help with testClass for a basic SOQL Class

Hey all,

 

I am looking to make a testClass for a apex class that is simply just querying information out of multiple objects.

 

It touches, Contact, ELP_Application__c and ELP_Assessment__c.

 

I have created a basic test, but it is not showing any coverage which is confusing...

 

Here is the code:

 

public class ELPScoreTracker {

    public ELP_Assessment__c EntryAssessment {get; private set;}
    public ELP_Assessment__c Assignment4 {get; private set;}
    public ELP_Assessment__c Assignment5 {get; private set;}
    public ELP_Assessment__c Assignment6 {get; private set;}
    public ELP_Assessment__c Assignment7 {get; private set;}
    public ELP_Assessment__c Assignment8 {get; private set;}
    public ELP_Assessment__c Assignment10 {get; private set;}
    
    public ELP_Assessment__c Perform1 {get; private set;}
    public ELP_Assessment__c Perform2 {get; private set;}
    public ELP_Assessment__c Perform3 {get; private set;}
    
    public ELP_Assessment__c FeedbackBuddy {get; private set;}
    
    public ELP_Assessment__c Average {get; private set;}
    
    public ELP_Assessment__c ELP_A {get; private set;}
    public ELP_Application__c ELP_C {get; private set;}
    public Contact ELP_U {get; private set;}
    
    public ELPScoreTracker(ApexPages.StandardController controller) {
        
    EntryAssessment = [Select Entry_B_Average_Score__c,
                            Entry_STAR_Average_Score__c
                              From ELP_Assessment__c 
                              Where Id = :ApexPages.CurrentPage().getParameters().get('id') LIMIT 1];   
        
    
    Assignment4 = [Select Assign4_AL_Average_Score__c,
                            Assign4_Final_Decision__c,
                            Assign4_PI_Average_Score__c,
                            Assign4_PM_Average_Score__c,
                            Assign4_STAR_Average_Score__c
                              From ELP_Assessment__c 
                              Where Id = :ApexPages.CurrentPage().getParameters().get('id') LIMIT 1];
                              
    Assignment5 = [Select Assign5_AL_Average_Score__c,
                            Assign5_Final_Decision__c,
                            Assign5_PI_Average_Score__c,
                            Assign5_PM_Average_Score__c,
                            Assign5_STAR_Average_Score__c
                              From ELP_Assessment__c 
                              Where Id = :ApexPages.CurrentPage().getParameters().get('id') LIMIT 1];

    Assignment6 = [Select Assign6_PI_Average_Score__c,
                            Assign6_Final_Decision__c
                              From ELP_Assessment__c 
                              Where Id = :ApexPages.CurrentPage().getParameters().get('id') LIMIT 1];
                              
    Assignment7 = [Select Assign7_AL_Average_Score__c,
                            Assign7_STAR_Average_Score__c,
                            Assign7_PI_Average_Score__c,
                            Assign7_PM_Average_Score__c,
                            Assign7_Final_Decision__c
                                From ELP_Assessment__c
                                Where Id = :ApexPages.CurrentPage().getParameters().get('id') LIMIT 1];

    Assignment8 = [Select Assign8_AL_Average_Score__c,
                            Assign8_STAR_Average_Score__c,
                            Assign8_PI_Average_Score__c,
                            Assign8_IPL_Average_Score__c,
                            Assign8_Final_Decision__c
                                From ELP_Assessment__c
                                Where Id = :ApexPages.CurrentPage().getParameters().get('id') LIMIT 1];
                                
    Assignment10 = [Select Assign10_AL_Average_Score__c,
                            Assign10_STAR_Average_Score__c,
                            Assign10_PI_Average_Score__c,
                            Assign10_IPL_Average_Score__c,
                            Assign10_Final_Decision__c
                                From ELP_Assessment__c
                                Where Id = :ApexPages.CurrentPage().getParameters().get('id') LIMIT 1];
                            
    Perform1 = [Select Perf_Convo1_B_Average_Score__c,
                            Perf_Convo1_RD_Average_Score__c,
                            Perf_Convo1_STAR_Average_Score__c,
                            Perf_Convo1_PI_Average_Score__c,
                            Perf_Convo1_IPL_Average_Score__c,
                            Perf_Convo1_Final_Decision__c
                              From ELP_Assessment__c 
                              Where Id = :ApexPages.CurrentPage().getParameters().get('id') LIMIT 1];     

    Perform2 = [Select Perf_Convo2_B_Average_Score__c,
                            Perf_Convo2_RD_Average_Score__c,
                            Perf_Convo2_AL_Average_Score__c,
                            Perf_Convo2_PI_Average_Score__c,
                            Perf_Convo2_Final_Decision__c
                              From ELP_Assessment__c 
                              Where Id = :ApexPages.CurrentPage().getParameters().get('id') LIMIT 1];
                              
/*  Perform3 = [Select Perf_Convo3_Final_Decision__c
                              From ELP_Assessment__c 
                              Where Id = :ApexPages.CurrentPage().getParameters().get('id') LIMIT 1];
                              */
    FeedbackBuddy = [Select Feedback_Buddy_IPL_Average_Score__c
                            From ELP_Assessment__c
                            Where Id = :ApexPages.CurrentPage().getParameters().get('id') LIMIT 1];   
                              
    Average = [Select Belief_Overall_Average__c,
                        Relentless_Drive_Overall_Average__c,
                        Adult_Leadership_Overall_Average__c,
                        Student_Achievement_Overall__c,
                        Personal_Improvement_Overall__c,
                        Project_Management_Overall_Average__c,
                        Interpersonal_Leadership_Overall__c
                        
                              From ELP_Assessment__c 
                              Where Id = :ApexPages.CurrentPage().getParameters().get('id') LIMIT 1];
                              
    ELP_A = [Select ELP_Application__c
                              From ELP_Assessment__c 
                              Where Id = :ApexPages.CurrentPage().getParameters().get('id') LIMIT 1];
                              
    ELP_C = [Select Applicant__c, CohortYear__c, Preferred_City__c
                              From ELP_Application__c 
                              Where Id = :ELP_A.ELP_Application__c LIMIT 1];
                              
    ELP_U = [Select LastName, FirstName
                              From Contact 
                              Where Id = :ELP_C.Applicant__c LIMIT 1];

}

public static testmethod void ELPScoreTracker() {
    

    
       Account acct = new Account(Name='test acct');
        insert acct;
        
        Contact testContact = new Contact(
            LastName = 'Test Contact'
        );
        testContact.accountId = acct.Id;
        insert testContact;
        
        ELP_Application__c testApp = new ELP_Application__c(
            Name = 'Test App',
            CohortYear__c = '2012-2013'
        );
        testApp.Applicant__c = testContact.Id;
        testApp.Application_Date__c = System.today();
        insert testApp;
        
        Contact Test_C = [Select LastName
                              From Contact 
                              Where Id = :testContact.Id LIMIT 1];
        
        ELP_Assessment__c testAss = new ELP_Assessment__c(
            Name = 'Test Assess',
            ELP_Application__c = testApp.Id,
            Entry_B_Average_Score__c = 4,
            Entry_STAR_Average_Score__c = 3
            
        );
        insert testAss;
        
            
            
            ELP_Application__c ELPApp = [SELECT Id FROM ELP_Application__c WHERE Id = :testAss.ELP_Application__c];
        
        
        
       System.assertEquals(testContact.LastName, Test_C.LastName);
       System.assertEquals(4, testAss.Entry_B_Average_Score__c);
       System.assertEquals(testAss.ELP_Application__c, ELPApp.Id);
        
}
}

I have worked with modifying much existing code and basic triggers and pieces, but never actually coded something this simple... If someone could give a little linsight, I'd really appreciate it.

ClintLeeClintLee

You haven't instantiated an ELPScoreTracker object in your test method, so the code is never run.  Are you meaning for this to be a Controller for a Visualforce page since you have a controller in the constructor argument?  

 

If not, just remove the ApexPages.StandardController controller from the constructor.  

 

After the last insert statement in your test class just instantiate a new ELPScoreTracker object like ELPScoreTracker tracker = new ELPScoreTracker();

 

Try running the test again and see what you get.

 

Hope that helps! 

 

~ Clint

 

 

 

ClintLeeClintLee

Scratch that last statement.  I see that you're referencing the 'id' parameter in the apex page.  Are you planning to use this as a controller extension?

 

kshannonkshannon

Yes, it is being referenced in the visualforce page ELPScoreTracker as well.