0

Hoping someone will be able to help me. I have come most of the way in trying to implement some code, but I have hit a road block. I am sitting right now at 66% code coverage, and I want to understand how to up this to at least 75%, if not 100%. I have been doing research for the last day and have made it this far, but I cannot find anything that else helpful on the subject. My class is:

public class janFebPromos {


 private final Account acct;
 Public list<AggregateResult> jfprom;
 public janFebPromos(ApexPages.StandardController stdController){
 this.acct = [select id, name from Account where id=:ApexPages.currentPage().getParameters().get('id')];
jfprom = [select report_description__c RDesc, SUM(qty_shipped__c) Quantity, SUM(Total_Sale_Amount__c) Total 
            from invitem__c 
            where cust_num__c = :this.acct.Id
            and sales_cat__c = 'PROM' 
            and inv_date__c >= 2016-12-15 
            and inv_date__c <= 2017-02-14 
            Group by report_description__c];
 }
public list<AggregateResult> jfpromlist {
get { return jfprom;}
}

}

And here is my test class so far:

@isTest
public class janFebPromosTest {
@isTest static void janFebPromosTest(){
    Account acct = new Account(Name='Test');
    invoice__c inv = new invoice__c(inv_num__c='123215654621');
    insert acct;
    insert inv;

    List<invitem__c> testILI = new List<invitem__c>();
    for(Integer i=0; i<10; i++){
        invitem__C o = new invitem__c();
        o.Ext_Id__c=String.valueOf(1234+i);          
        o.inv_date__c=date.valueOf('2017-01-01');
        o.cust_num__c= acct.id;
        o.inv_num__c=inv.Id;
        o.sales_cat__c=string.valueOf('PROM');
        o.report_description__c=string.valueof('1 test');
        o.qty_shipped__c=decimal.valueof('5');
        testILI.add(o);
    }
    insert testILI;

    ApexPages.currentPage().getParameters().put('id',acct.Id);
    ApexPages.StandardController std = new ApexPages.StandardController(acct);
    janFebPromos ace = new janFebPromos(std);

}

}

Any guidance would be greatly appreciated!!

1 Answer 1

2

You are not even using acct variable anywhere, if required declare it inside the constructor.


Following line would give List has no row of assignment error when a valid Id is not passed in URL or if empty.

Use ApexPages.currentPage().getParameters().get('id') directly inside the query.


Your test method doesn't even has any asserts, apart from coverage add as much as meaningful asserts possible.

Adding a psuedo test code for you, important is to call the property jfpromlist which would cover it. Do not forget to validate the result by adding appropriate asserts.

@isTest public class janFebPromosTest {
    @isTest static void janFebPromosTest(){

        // your test data creation
        //**** Use asserts to verify your test data is been created, NEVER assume!

        // start actual testing inside Start and stop test
        Test.startTest();

        // calling controller method
        ApexPages.currentPage().getParameters().put('id',acct.Id);
        ApexPages.StandardController std = new ApexPages.StandardController(acct);
        janFebPromos ace = new janFebPromos(std);

        // fetch the list of aggregate result from controller's instance
        List<AggregateResult> jfpromlist = ace.jfpromlist;

        // assert your result
        System.assertEquals('your condition',
            jfpromlist);

        Test.stopTest;
    }

}
3
  • your suggestions are wonderful I really appreciate your help! I am still in the early stages of learning Apex, can you please explain what the 'your condition' mean in the final 'System.assertEquals()'? I have been trying to pin it down, but have been unable to when it comes to a list of results.
    – Brio87
    Commented Jul 11, 2017 at 21:51
  • Please refer this trailhead module. Its pretty much extensive. In short, asserts are used to validate if you're getting expected data as if you were testing it manually.
    – Raul
    Commented Jul 11, 2017 at 22:51
  • Cool, I really appreciate it, Rahul!
    – Brio87
    Commented Jul 12, 2017 at 13:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .