You need to sign in to do that
Don't have an account?
Bahri Yucesan
How can i write a good Unit Test Class ?
Hi all,
I build a Class and its Unit Test Class as follows but Code Coverage %38. How can i pass %75.
Class
My Test Units
I build a Class and its Unit Test Class as follows but Code Coverage %38. How can i pass %75.
Class
public class AccountTrainerRelaionController { public Boolean Show { get; set; } public List<Attendees__c> trainerList { get; set; } private ApexPages.StandardController std; public Class__c a {get; set;} public List<TrainerClass> TrainerClassList {get; set;} public AccountTrainerRelaionController(ApexPages.StandardController controller) { std = controller; } public PageReference Search() { Show=true; a = (Class__c) std.getRecord(); String TrainerCheck ='name'; List<Attendees__c> results = [select Class__r.Trainer__r.Name , Class__r.Trainer__r.Id , Class__r.Instruction__r.Name , Class__r.AttendeeCount__c , Class__r.Id , Class__r.Name, Class__r.Baslangic_Tarihi__c , Name , Id , Contact__r.Name , Contact__r.Id , Contact__r.Title , Contact__r.Email from Attendees__c where Contact__r.Account.Id =:a.Account__c and Class__r.Trainer__c !=null order by Class__r.Trainer__r.Name,Class__r.Id ]; List<ClassClass> cclist = new List<ClassClass>(); TrainerClassList = new List<TrainerClass>(); //TrainerClass tc = new TrainerClass(); List<Attendees__c> attList = new List<Attendees__c>(); ClassClass cc = new ClassClass(); List<TrainerClass> tcList = new List<TrainerClass>(); TrainerClass tc = new TrainerClass(); for(Attendees__c c : results) { if (TrainerCheck != c.Class__r.Trainer__r.Name ) { tc = new TrainerClass(); tc.TrainerName = c.Class__r.Trainer__r.Name; tc.TrainerId = c.Class__r.Trainer__r.Id; tcList.add(tc); TrainerClassList.add(tc); TrainerCheck = c.Class__r.Trainer__r.Name; } } List<ClassClass> clList = new List<ClassClass>(); ClassClass cl = new ClassClass(); List<Attendees__c> atdList = new List<Attendees__c>(); Attendees__c atd = new Attendees__c(); for(TrainerClass t : TrainerClassList) { for(Attendees__c c : results) { if (t.TrainerId == c.Class__r.Trainer__r.Id ) { cl = new ClassClass(); cl.ClassName = c.Class__r.Name; cl.ClassId = c.Class__r.Id; cl.CourseName = c.Class__r.Instruction__r.Name; cl.AttendeeCount = c.Class__r.AttendeeCount__c; cl.BaslangicTarihi = c.Class__r.Baslangic_Tarihi__c; clList.add(cl); } else { t.Classes = clList; } } clList = new List<ClassClass>(); for(ClassClass c : t.Classes) { atdList = new List<Attendees__c>(); for(Attendees__c a : results) { if( c.ClassId == a.Class__r.Id ) {atdList.add(a);} } c.Attendees = atdList; } } return null; } public class TrainerClass{ public String TrainerName{get; set;} public String TrainerId{get; set;} public List<ClassClass> Classes{get; set;} } public class ClassClass{ public String ClassName{get; set;} public String ClassId{get; set;} public String CourseName{get; set;} public Decimal AttendeeCount{get; set;} public Date BaslangicTarihi{get; set;} public Integer ClassCount{get; set;} public List<Attendees__c> Attendees{get; set;} } }
My Test Units
@isTest public class AccountTrainerRelaionControllerTest { static testMethod void testSearch() { Courses__c course = new Courses__c(); course.Name='TESTCOURSE'; insert course; Trainer__c tc = new Trainer__c(); tc.Egitmen_Adi__c= 'test'; tc.Egitmen_Soyadi__c = 'deneme'; insert tc; Class__c cl = new Class__c(); cl.Instruction__c = course.Id; cl.Baslangic_Tarihi__c = System.today(); cl.Bitis_Tarihi__c = System.today(); cl.Egitim_Suresi__c = 1; insert cl; ApexPages.StandardController controller = new ApexPages.StandardController(cl); AccountTrainerRelaionController cont = new AccountTrainerRelaionController(controller); cont.a=cl; cont.Search(); //call method //PageReference pageReference = cont.Search(); System.assertEquals(0,cont.TrainerClassList.size()); //get url String url = '/apex/AccountTrainerRelation'; //is not equal System.AssertEquals(url ,pageReference.getUrl()); } }Thanks
It is because you have not created dummy data for Account, Attendee object. Kindly create it and then try.
In case, you face any issue, drop me message on forum or Skype me @mohit_bansal17, if you need any help.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
Hi Bahri,
I hope this answers your question.
In your Test Method you assert that there are 0 records:
If there are 0 TrainerClass records you will not loop through the for loop:
You should add bulk data to test methods when you have bulk DML operations.
In this scenario you should add enough data to traverse your code.
You should also test as many +ve and -ve scenarios as your code requires:
For example you have an IF ELSE, which has 2 specific paths. You should test both paths.
In this example you need to have some data where t.TrainerId == c.Class__r.Trainer__r.Id, and some data where they do not equal (for the ELSE)
Enjoy!
Please try below code :-
NOTE:- Always follow below best pratice for test classes.
1) Make calls to methods using both valid and invalid inputs.
2) Complete successfully without throwing any exceptions, unless those errors are expected and caught in a try…catch block.
3) Always handle all exceptions that are caught, instead of merely catching the exceptions.
4) Use System.assert methods to prove that code behaves properly.
5) Use the runAs method to test your application in different user contexts.
6) Exercise bulk trigger functionality—use at least 20 records in your tests.
Please refer below link for test classes
http://https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
http://blog.shivanathd.com/2013/11/Best-Practices-Test-Class-in-Salesforce.html
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
Thanks
Amit Chaudhary
amit.salesforce21@gmail.com