You need to sign in to do that
Don't have an account?
TheLearner
Improving the code coveragae
Hi Experts,
Could anyone improve my code coverage, i made red in color lines which are not covered. i mentioned the code from whr to whr not covering could anyone help me please
Could anyone improve my code coverage, i made red in color lines which are not covered. i mentioned the code from whr to whr not covering could anyone help me please
public with sharing class WPD_Schedule_4Controller { List<WPD_Condition__c> conditionList = new List<WPD_Condition__c>(); List<WPD_Co_Ordinate__c> coOrdinateList = new List<WPD_Co_Ordinate__c>(); List<WPD_Purchase_Order_Line_Item__c> coPOList = new List<WPD_Purchase_Order_Line_Item__c>(); public WPD_Projects__c Proj; public WPD_Reinstatement__c Work; public WPD_Co_Ordinate__c coOrdinate; public WPD_Purchase_Orders__c poLineItems; public WPD_Post_Code__c postCodeRec; public boolean modulePresent = false; public WPD_Reinstatement__c getWork(){ if (Work==null){ Work= new WPD_Reinstatement__c(); } return Work; } public WPD_Projects__c getProject() { if (Proj==null){ Proj = new WPD_Projects__c(); } return Proj; } public List<WPD_Condition__c> getArrCondition() { //Add to collection only if empty, else if mandatory field error comes, it will keep adding same numbers on VF page if (conditionList.size()==0) { for (integer i=0; i<=12; i++) { conditionList.add(new WPD_Condition__c()); } } return conditionList; } public List<WPD_Co_Ordinate__c> getArrCoordinate() { //Add to collection only if empty, else if mandatory field error comes, it will keep adding same numbers on VF page if (coOrdinateList.size()==0) { for (integer i=1; i<=6; i++) { coOrdinateList.add(new WPD_Co_Ordinate__c()); } } return coOrdinateList; } public List<WPD_Purchase_Order_Line_Item__c> getArrPurchaseOrder() { //Add to collection only if empty, else if mandatory field error comes, it will keep adding same numbers on VF page if (coPOList.size()==0) { for (integer i=1; i<=20; i++)//CH03 { coPOList.add(new WPD_Purchase_Order_Line_Item__c()); } } return coPOList; } public PageReference save() { //Create savepoint Savepoint sp; List<WPD_Projects__c> WPDProj = [select Id, Name from WPD_Projects__c where Name =:Proj.Name]; //CH04.Start Date estimatePlanDate = Date.newInstance(2014,10,31); System.debug('estimatePlanDate = '+estimatePlanDate); List<WPD_Contract__c> wpdContracts; if(Proj.Estimated_Project_Start_Date__c != null && Proj.Work_Type__c != null) wpdContracts = new List<WPD_Contract__c>([SELECT Id,Name,Work_Type__c,End_Date__c FROM WPD_Contract__c WHERE Work_Type__c INCLUDES (:Proj.Work_Type__c) AND End_Date__c >=: Proj.Estimated_Project_Start_Date__c order by End_Date__c ASC limit 1 ]);//CH05 else wpdContracts = new List<WPD_Contract__c>(); System.debug((Proj.Estimated_Project_Start_Date__c >= estimatePlanDate)+','+wpdContracts.isEmpty()); //CH04.End if(WPDProj.size() > 0) { ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'You can not save as this would create a duplicate record with Work Instruction field value [' + proj.Name + '].'); ApexPages.addMessage(msg); return NULL; } //CH02.Start else if((Proj.Work_Type__c == 'Planned' || Proj.Work_Type__c == 'Planned GSOS') && (Proj.Dig_Start_Date__c == null || Proj.Dig_End_Date__c == null)){ ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Dig Start Date and Dig End Date are mandatory if work type is Planned or Planned GSOS'); ApexPages.addMessage(msg); return NULL; }//CH02.End //CH04.Start else if(Proj.Estimated_Project_Start_Date__c != null && estimatePlanDate >= Proj.Estimated_Project_Start_Date__c && !wpdContracts.isEmpty() && wpdContracts.size() > 1 ){ @@@@@@@@@@@--NOt covering.Start---@@@@@@@@@@@@ ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Combination of Work Type and Estimated Start date does not match one individual current Contract Number record'); ApexPages.addMessage(msg); return NULL; @@@@@@@-- Not covering.End----@@@@@@@@@@@@@@@@ }else if(Proj.Estimated_Project_Start_Date__c != null && Proj.Estimated_Project_Start_Date__c >= estimatePlanDate && wpdContracts.isEmpty()){ ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'No Contract Number found with Work Type and End Date > Estimated Project Start Date.'); ApexPages.addMessage(msg); return NULL; }else{//CH04.End try{ sp = Database.setSavepoint(); if(Proj.Estimated_Project_Start_Date__c != null && estimatePlanDate <= Proj.Estimated_Project_Start_Date__c && !wpdContracts.isEmpty()){ Proj.Contract__c = wpdContracts.get(0).id; } Proj.Date_Received__c = Date.today(); insert Proj; work.WPD_Projects__c = Proj.Id; work.Works_Start_Date__c = work.Estimated_Start__c; work.Works_End_Date__c = work.Estimated_End__c; insert work; //Save Co-ordinate data for(integer i=0; i<6; i++){ if(coOrdinateList[i].Easting__c!=null || coOrdinateList[i].Northing__c!=null){ @@@@@@@@@@------Not covering.start----/@@@@@@@@@@@@@@@@@ coOrdinateList[i].Work__c = Work.Id; if(i==0){ work.Eastings_Start__c=coOrdinateList[i].Easting__c; work.Northings_Start__c=coOrdinateList[i].Northing__c; }else if(i==1){ work.Eastings_End__c=coOrdinateList[i].Easting__c; work.Northings_End__c=coOrdinateList[i].Northing__c; } } } insert coOrdinateList; update work; for (integer i=0; i<20; i++){ if (coPOList[i].Module__c!=null) { modulePresent=true; } } if(modulePresent){ //Save Purchase Order poLineItems = new WPD_Purchase_Orders__c(); poLineItems.WPD_Projects__c = Proj.Id; poLineItems.Purchase_Order_No__c = Proj.Name; poLineItems.Date_Received__c = Date.today(); insert poLineItems; //Save Purchase Order Line Item records for (integer i=0; i<20; i++){ if (coPOList[i].Module__c!=null) { coPOList[i].PO_Number__c = poLineItems.Id; //coPOList[i].Ordered_Rate__c = coPOList[i].Module__r.Rate__c; //Populate Ordered Rate with Rate from Module object //CH02 commented this line ///coPOList[i].Category__c = 'Not Required'; //Default Category to ‘Not Required’ ///coPOList[i].Authorisation__c = true; ///coPOList[i].PO_Item__c = '001'; } } insert coPOList; } //CH02.Start //Create WPD Task Record if(poLineItems != null){ WPD_Purchase_Orders__c PO = [select Purchase_Order_Value__c, Purchase_Order_Duration__c from WPD_Purchase_Orders__c where id=:poLineItems.id]; WPD_Task__c task = new WPD_Task__c(); task.WPD_Project_Work_Instruction__c = Proj.Id; task.value__c = PO.Purchase_Order_Value__c; task.Duration__c = PO.Purchase_Order_Duration__c; insert task; //CH02.End } PageReference SchPage = new PageReference('/' + Proj.Id); //Redirect to the project record created SchPage.setRedirect(true); return SchPage; @@@@@@@@@@ ----Not covering. End-----@@@@@@@@@@@@@@@@ }catch(Exception ex){ Database.rollback(sp); Proj = Proj.clone(false); work = work.clone(false); List<WPD_Co_Ordinate__c> templstCoOrdinates = coOrdinateList.clone(); coOrdinateList.clear(); for(WPD_Co_Ordinate__c tmpCoordinate : templstCoOrdinates){ coOrdinateList.add(tmpCoordinate.clone(false)); } ///ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,String.valueOf(ex)); ///ApexPages.addMessage(msg); ApexPages.addMessages(ex); return NULL; } } } //this is for rnd n debugging public PageReference cancel() { PageReference SchPage1 = new PageReference('/home/home.jsp' );//ApexPages.currentPage(); return SchPage1; } } public with sharing class WPD_Schedule_4Controller { List<WPD_Condition__c> conditionList = new List<WPD_Condition__c>(); List<WPD_Co_Ordinate__c> coOrdinateList = new List<WPD_Co_Ordinate__c>(); List<WPD_Purchase_Order_Line_Item__c> coPOList = new List<WPD_Purchase_Order_Line_Item__c>(); public WPD_Projects__c Proj; public WPD_Reinstatement__c Work; public WPD_Co_Ordinate__c coOrdinate; public WPD_Purchase_Orders__c poLineItems; public WPD_Post_Code__c postCodeRec; public boolean modulePresent = false; public WPD_Reinstatement__c getWork(){ if (Work==null){ Work= new WPD_Reinstatement__c(); } return Work; } public WPD_Projects__c getProject() { if (Proj==null){ Proj = new WPD_Projects__c(); } return Proj; } public List<WPD_Condition__c> getArrCondition() { //Add to collection only if empty, else if mandatory field error comes, it will keep adding same numbers on VF page if (conditionList.size()==0) { for (integer i=0; i<=12; i++) { conditionList.add(new WPD_Condition__c()); } } return conditionList; } public List<WPD_Co_Ordinate__c> getArrCoordinate() { //Add to collection only if empty, else if mandatory field error comes, it will keep adding same numbers on VF page if (coOrdinateList.size()==0) { for (integer i=1; i<=6; i++) { coOrdinateList.add(new WPD_Co_Ordinate__c()); } } return coOrdinateList; } public List<WPD_Purchase_Order_Line_Item__c> getArrPurchaseOrder() { //Add to collection only if empty, else if mandatory field error comes, it will keep adding same numbers on VF page if (coPOList.size()==0) { for (integer i=1; i<=20; i++)//CH03 { coPOList.add(new WPD_Purchase_Order_Line_Item__c()); } } return coPOList; } public PageReference save() { //Create savepoint Savepoint sp; List<WPD_Projects__c> WPDProj = [select Id, Name from WPD_Projects__c where Name =:Proj.Name]; //CH04.Start Date estimatePlanDate = Date.newInstance(2014,10,31); System.debug('estimatePlanDate = '+estimatePlanDate); List<WPD_Contract__c> wpdContracts; if(Proj.Estimated_Project_Start_Date__c != null && Proj.Work_Type__c != null) wpdContracts = new List<WPD_Contract__c>([SELECT Id,Name,Work_Type__c,End_Date__c FROM WPD_Contract__c WHERE Work_Type__c INCLUDES (:Proj.Work_Type__c) AND End_Date__c >=: Proj.Estimated_Project_Start_Date__c order by End_Date__c ASC limit 1 ]);//CH05 else wpdContracts = new List<WPD_Contract__c>(); System.debug((Proj.Estimated_Project_Start_Date__c >= estimatePlanDate)+','+wpdContracts.isEmpty()); //CH04.End if(WPDProj.size() > 0) { ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'You can not save as this would create a duplicate record with Work Instruction field value [' + proj.Name + '].'); ApexPages.addMessage(msg); return NULL; } //CH02.Start else if((Proj.Work_Type__c == 'Planned' || Proj.Work_Type__c == 'Planned GSOS') && (Proj.Dig_Start_Date__c == null || Proj.Dig_End_Date__c == null)){ ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Dig Start Date and Dig End Date are mandatory if work type is Planned or Planned GSOS'); ApexPages.addMessage(msg); return NULL; }//CH02.End //CH04.Start else if(Proj.Estimated_Project_Start_Date__c != null && estimatePlanDate >= Proj.Estimated_Project_Start_Date__c && !wpdContracts.isEmpty() && wpdContracts.size() > 1 ){ not cover /* ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'Combination of Work Type and Estimated Start date does not match one individual current Contract Number record'); ApexPages.addMessage(msg); return NULL;*/ not cover }else if(Proj.Estimated_Project_Start_Date__c != null && Proj.Estimated_Project_Start_Date__c >= estimatePlanDate && wpdContracts.isEmpty()){ ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'No Contract Number found with Work Type and End Date > Estimated Project Start Date.'); ApexPages.addMessage(msg); return NULL; }else{//CH04.End try{ sp = Database.setSavepoint(); if(Proj.Estimated_Project_Start_Date__c != null && estimatePlanDate <= Proj.Estimated_Project_Start_Date__c && !wpdContracts.isEmpty()){ Proj.Contract__c = wpdContracts.get(0).id; } Proj.Date_Received__c = Date.today(); insert Proj; work.WPD_Projects__c = Proj.Id; work.Works_Start_Date__c = work.Estimated_Start__c; work.Works_End_Date__c = work.Estimated_End__c; insert work; //Save Co-ordinate data for(integer i=0; i<6; i++){ if(coOrdinateList[i].Easting__c!=null || coOrdinateList[i].Northing__c!=null){ not cover /* coOrdinateList[i].Work__c = Work.Id; if(i==0){ work.Eastings_Start__c=coOrdinateList[i].Easting__c; work.Northings_Start__c=coOrdinateList[i].Northing__c; }else if(i==1){ work.Eastings_End__c=coOrdinateList[i].Easting__c; work.Northings_End__c=coOrdinateList[i].Northing__c; } } } insert coOrdinateList; update work; for (integer i=0; i<20; i++){ if (coPOList[i].Module__c!=null) { modulePresent=true; } } if(modulePresent){ //Save Purchase Order poLineItems = new WPD_Purchase_Orders__c(); poLineItems.WPD_Projects__c = Proj.Id; poLineItems.Purchase_Order_No__c = Proj.Name; poLineItems.Date_Received__c = Date.today(); insert poLineItems; //Save Purchase Order Line Item records for (integer i=0; i<20; i++){ if (coPOList[i].Module__c!=null) { coPOList[i].PO_Number__c = poLineItems.Id; //coPOList[i].Ordered_Rate__c = coPOList[i].Module__r.Rate__c; //Populate Ordered Rate with Rate from Module object //CH02 commented this line ///coPOList[i].Category__c = 'Not Required'; //Default Category to ‘Not Required’ ///coPOList[i].Authorisation__c = true; ///coPOList[i].PO_Item__c = '001'; } } insert coPOList; } //CH02.Start //Create WPD Task Record if(poLineItems != null){ WPD_Purchase_Orders__c PO = [select Purchase_Order_Value__c, Purchase_Order_Duration__c from WPD_Purchase_Orders__c where id=:poLineItems.id]; WPD_Task__c task = new WPD_Task__c(); task.WPD_Project_Work_Instruction__c = Proj.Id; task.value__c = PO.Purchase_Order_Value__c; task.Duration__c = PO.Purchase_Order_Duration__c; insert task; //CH02.End } PageReference SchPage = new PageReference('/' + Proj.Id); //Redirect to the project record created SchPage.setRedirect(true); return SchPage; */ not cover }catch(Exception ex){ Database.rollback(sp); Proj = Proj.clone(false); work = work.clone(false); List<WPD_Co_Ordinate__c> templstCoOrdinates = coOrdinateList.clone(); coOrdinateList.clear(); for(WPD_Co_Ordinate__c tmpCoordinate : templstCoOrdinates){ coOrdinateList.add(tmpCoordinate.clone(false)); } ///ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,String.valueOf(ex)); ///ApexPages.addMessage(msg); ApexPages.addMessages(ex); return NULL; } } } //this is for rnd n debugging public PageReference cancel() { PageReference SchPage1 = new PageReference('/home/home.jsp' );//ApexPages.currentPage(); return SchPage1; } }
Why are you testing whether the size of the wpdContracts is greater than 1 where as you already tested the size is not empty. The condition is wrong itself. Please check rest of the conditions with your business requirement and code accordingly.
For 100% test coverage you can consult this article https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests
Hope this helps!
Then consult your primary requirement to refine the different conditions
Thanks for the quick reply and also sorry for the late reply as i mentioned its already existing code in the live so i cant change it . could you help me in improving the code. please