1
votes

I'm having a tough time trying to get all code covered in my test class for an apex class.

Apex class:

public with sharing class myclass {

**public List<CustomObject1> listvar {get;set;}**

public myclass(ApexPages.StandardController sc){
    CustomObject2 var = [SELECT Id, Field1__c FROM CustomObject2 WHERE Id = :ApexPages.currentPage().getParameters().get('id')]; 
    **listvar = [SELECT Id,Name,Field1__c,Field2__c,Field3__c,Field4__c,Field5__c,CreatedDate,CreatedById FROM CustomObject1 WHERE Field2__c = :var.Field1__c ORDER BY CreatedDate DESC];**
}

}

Test Class:

@isTest
public class myclass_Test {

     static testmethod void dosomething(){
Account a = new Account();
            a.Name = 'Test acct';
            insert a;

        CustomObject4__c v = new CustomObject4__c();
            v.Field1__c = '123 ABC';
            v.Name = 'test name';
            v.Field2__c = True;
            v.Account__c = a.Id;
            insert v;
... more record creates including ones for the object being queried...

PageReference pageref = Page.myVFpage;
    Test.setCurrentPageReference(pageref);
    ApexPages.StandardController sc = new ApexPages.StandardController(v); 

        myclass myPageCon = new myclass(sc);
}
}

I've tried creating a new list for the underneath the last line in the test class and populating the list, but I cannot get 100% code coverage. I marked the lines that I'm not getting any coverage from the test class with. Any suggestions?

2
Which are the marked lines? The lines start with the **?Noor A Shuvo

2 Answers

0
votes

You should put some asserts into your test Class. Something like

System.assertEquals(5, yourListsize)
0
votes

I figured out that the listvar list for CustomObject1 wasn't getting populated because an Id wasn't being passed to var for CustomObject2. In the test class I had to put the record Id using ApexPages.currentPage().getParameters().put('Id', something.id);

with the Id for the record created in the test class for that object. Thanks anyways guys :-)