You need to sign in to do that
Don't have an account?
William Keam
Need help with Apex Testing
Hello everyone,
I need a hand getting 100% code coverage for one of my Apex Classes
The Apex Class:
The Test Class
Im still fairly new to Apex and would appreciate any and all help with this.
Thanks in advance!
I need a hand getting 100% code coverage for one of my Apex Classes
The Apex Class:
public class avgCES{ private final Case cas; public double avrgCES{public get; public set;} public string CES_Colour{public get; public set;} public double count{public get; public set;} public avgCES(ApexPages.StandardController controller) { this.cas = (Case)Controller.getRecord(); if(cas.account.id!=NULL){ List <case> allCase=[Select How_easy_was_it_to_get_the_help_you_need__c from case where accountid=:cas.account.id and How_easy_was_it_to_get_the_help_you_need__c!=NULL]; double d=0.0; integer count=0; if(allCase.size()>0){ for(case c: allCase) { d+=c.How_easy_was_it_to_get_the_help_you_need__c; count++; } } if(count>0) { avrgCES=d/count; } else { avrgCES=0; CES_Colour='Grey'; } if(avrgCES<6) { CES_Colour='Red'; } else if(avrgCES>=6 && avrgCES <=8 ) { CES_Colour='Yellow'; } else { CES_Colour='Green'; } } } }
The Test Class
@isTest public class avgCES_test{ public static testMethod void testAvgCES() { Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1); insert a; Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=6); Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=5); insert c; insert c2; ApexPages.StandardController sc = new ApexPages.standardController(c); avgCES testme = new avgCES(sc); testme.avrgCES=5.5; testme.CES_Colour='Red'; system.assertEquals(5.5, testme.avrgCES); system.assertEquals('Red', testme.CES_Colour); } public static testMethod void testAvgCES2() { Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1); insert a; Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=10); Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=8); insert c; insert c2; ApexPages.StandardController sc2 = new ApexPages.standardController(c); avgCES testme = new avgCES(sc2); testme.avrgCES=9; system.assertEquals(9, testme.avrgCES); system.assertEquals('Green', testme.CES_Colour); } public static testMethod void testAvgCES3() { Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1); insert a; Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=7); Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=8); insert c; insert c2; ApexPages.StandardController sc3 = new ApexPages.standardController(c); avgCES testme = new avgCES(sc3); testme.avrgCES=7.5; testme.CES_Colour='Yellow'; system.assertEquals(7.5, testme.avrgCES); system.assertEquals('Yellow', testme.CES_Colour); } public static testMethod void testAvgCES4() { Account a = new Account(name='TestAcc',Reseller_Id__c=12345,Virtualisation_Id__c=1); insert a; Case c = new Case(subject='Test Case',Account=a,How_easy_was_it_to_get_the_help_you_need__c=NULL); Case c2 = new Case(subject='Test Case2',Account=a,How_easy_was_it_to_get_the_help_you_need__c=NULL); insert c; insert c2; ApexPages.StandardController sc4 = new ApexPages.standardController(c); avgCES testme = new avgCES(sc4); testme.avrgCES=0; testme.CES_Colour='Grey'; system.assertEquals(0, testme.avrgCES); system.assertEquals('Grey', testme.CES_Colour); } }
Im still fairly new to Apex and would appreciate any and all help with this.
Thanks in advance!
Manh
All Answers
Add this method into your test class :
Manh
Thanks for the help so far unfortunately that test only gave me 30% coverage
Manh
100% code coverage!
Again, thank you so much for your help!