I think I made the test in the right way, but coverage still 31%
public class ContractMilestoneInfoApex {
public List<Milestoneinfo> MSIList {set; get;}
public Milestoneinfo MSI {set; get;}
public List<Work_Order_Task__c> query {get; set;}
public String currentRecordId {get;set;}
public ContractMilestoneInfoApex (ApexPages.StandardController controller){
String url = ApexPages.currentPage().getURL();
currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
MSIList = new List<Milestoneinfo>();
query = new List<Work_Order_Task__c>();
query = [select Name,Task_Type__c,Man_Days_Estimated__c,Man_Days_Actual__c,(select Resource_Name__c,Estimated_Man_Days__c,Actual_Man_Days__c from Resources__r) from Work_Order_Task__c where Milestone__c = :currentRecordId];
Integer n = 1;
for(Work_Order_Task__c m: query){
MSI = new Milestoneinfo();
MSI.num = n++;
MSI.name = m.Name;
MSI.tasktype = m.Task_Type__c;
MSI.estimated_MD = m.Man_Days_Estimated__c;
MSI.actual_MD = m.Man_Days_Actual__c;
MSI.RESList = new List<Resourceinfo>();
for(Resource__c res: m.Resources__r){
Resourceinfo resinfo = new Resourceinfo();
resinfo.res_name = res.Resource_Name__c;
resinfo.res_estimated_MD = res.Estimated_Man_Days__c;
resinfo.res_actual_MD = res.Actual_Man_Days__c;
MSI.RESList.add(resinfo);
}
MSIList.add(MSI);
}
}
public class Milestoneinfo{
public Integer num{get; set;}
public String name{get; set;}
public String tasktype{get; set;}
public Double estimated_MD{get; set;}
public Double actual_MD{get; set;}
public List<Resourceinfo> RESList{get; set;}
}
public class Resourceinfo{
public String res_name{get; set;}
public Double res_estimated_MD{get; set;}
public Double res_actual_MD{get; set;}
}
}
@isTest
public class ContractMilestoneInfoApexTest{
static testMethod void myTest() {
list<Milestoneinfo> MSIList = new List<Milestoneinfo>();
Account acc = new Account(Name = 'TEST Account12345');
insert acc;
Contract contr = new Contract();
contr.Contract_Title__c = 'Test Contract';
contr.StartDate = system.today();
contr.EndDate = system.today() + 365;
contr.Received_Date__c = system.today();
contr.Contract_Auto_Number__c = 1038;
contr.Contract__c = 'Draft';
contr.AccountId = acc.id;
upsert contr;
PO_Invoices__c INO = new PO_Invoices__c();
INO.Status__c = 'Planned';
INO.Notes__c = 'SDP Preparation Description';
INO.Issue_Date__c = Date.Today() + 10;
INO.Paid_Amount__c = 0;
INO.Total_Amount__c = 400000;
INO.Choose_Technical_Leader__c = 'Primary Technical Leader';
INO.PO__c = contr.id;
INO.Technical_Work_Status__c = 'Delivered';
upsert INO;
Work_Order__c wo = new Work_Order__c();
wo.Client__c = acc.id;
wo.Status__c = 'New';
wo.Start_Date__c = Date.Today()+1;
wo.End_Date__c = Date.Today()+4;
wo.Type__c = 'Service Request';
upsert wo;
Profile p = [SELECT Id FROM Profile WHERE Name = '!Changi - System Admin'];
ID UserId = UserInfo.getUserId();
User u = new User(
Alias = 'Khan',
Email = '[email protected]',
EmailEncodingKey = 'UTF-8',
LastName = 'Test',
LanguageLocaleKey = 'en_US',
LocaleSidKey = 'en_US',
TimeZoneSidKey='Asia/Dubai',
ProfileId = p.Id,
UserName='[email protected]',
Resource_Manager__c = UserId
);
INSERT u;
Work_Order_Task__c SDRSubTask3 = new Work_Order_Task__c();
SDRSubTask3.Name = 'test sub task';
SDRSubTask3.Task_Type__c = 'Technical Lead Coordination / Status Reporting';
SDRSubTask3.Man_Days_Estimated__c = 100;
SDRSubTask3.Man_Days_Actual__c = 100;
SDRSubTask3.Work_Order__c = wo.id;
SDRSubTask3.Status__c = 'Planned';
SDRSubTask3.Approved_by_Client__c = false;
SDRSubTask3.Milestone__c = INO.id;
upsert SDRSubTask3;
Resource__c r1 = new Resource__c();
r1.Work_Order_Task__c = SDRSubTask3.id;
r1.Proposed_Employee__c = u.id;
r1.Employee__c = u.id;
r1.Deducted_Man_Days__c = 100.0;
r1.Estimated_Man_Days__c = 100.0;
r1.Actual_Man_Days__c = 100.0;
upsert r1;
Work_Order_Task__c SDRSubTask4 = new Work_Order_Task__c();
SDRSubTask4.Name = 'test sub task1';
SDRSubTask3.Task_Type__c = 'Technical Lead Coordination / Status Reporting';
SDRSubTask3.Man_Days_Estimated__c = 100;
SDRSubTask3.Man_Days_Actual__c = 100;
SDRSubTask4.Work_Order__c = wo.id;
SDRSubTask4.Milestone__c = INO.id;
SDRSubTask4.Status__c = 'In Progress';
upsert SDRSubTask4;
Resource__c r2 = new Resource__c();
r2.Work_Order_Task__c = SDRSubTask4.id;
r2.Proposed_Employee__c = u.id;
r2.Employee__c = u.id;
r2.Deducted_Man_Days__c = 100.0;
r2.Estimated_Man_Days__c = 100.0;
r2.Actual_Man_Days__c = 100.0;
upsert r2;
Test.startTest();
PageReference pageRef = Page.ContractMilestoneInfo;
ApexPages.currentPage().getParameters().put('id', String.valueOf(INO.id));
Test.setCurrentPage(pageRef);
ContractMilestoneInfoApex accopp = new ContractMilestoneInfoApex (new ApexPages.StandardController(INO));
Test.stopTest();
}
public class Milestoneinfo{
public Integer num{get; set;}
public String name{get; set;}
public String tasktype{get; set;}
public Double estimated_MD{get; set;}
public Double actual_MD{get; set;}
public List<Resourceinfo> RESList{get; set;}
}
public class Resourceinfo{
public String res_name{get; set;}
public Double res_estimated_MD{get; set;}
public Double res_actual_MD{get; set;}
}
}