function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Srinivas CSrinivas C 

Hi iam unable to cover the test coverage for this lines of code please help

newEngagement = new Projects__c(Account__c = opp.Account_Name__c, Opportunityc__c = opp.Id, Name = opp.Name + ' - ' + System.today().format(),Project_Manager__c=opp.Project_Manager_list__c,End_Date__c=opp.End_Date__c);
        

list<Projects__c> upEngagement=[select id,name from Projects__c where id in: templateIds];
            newEngagement.Estimated_Project_Revenue__c = totalAmount;
            for(Projects_ESR__c templist:upEngagement)
            {
                system.debug('templateIds'+templatename);
                templatename+=templist.Name+',';
            }
            templatename=templatename.removeEnd(',');
            newEngagement.Template_Project__c=templatename;
            
            
            salesres=[select id,name,Sales_Responsible_Name__c,Email from user where Sales_Responsible_Name__c!=null and Sales_Responsible_Name__c=:newEngagement.Project_Manager__c];//'Anthony Kent'];//opp.Sales_Responsible__c];// id=:sales];
            if(salesres.size()>0){
                
                
                newEngagement.Sales_responsible_email__c=salesres[0].email;
            }
            newEngagement.Total_Sales_Price__c=opp.Total_Sales_Price__c;
            newEngagement.Labour_Cost__c=opp.Labour_Cost__c;
            newEngagement.External_Cost__c=opp.External_Cost__c;
            newEngagement.Contingency_Cost__c=opp.Contingency_Cost__c;
            
            insert newEngagement;
                        PageReference pr = new PageReference('/' + newEngagement.Id);
            pr.setRedirect(true);
            return pr;
        }

Regards,
Srinivas C
Malika Pathak 9Malika Pathak 9

Hi Srinivas,

Please Find The Solution.

Test Class

@isTest
public class projectApex_Test {
    @istest static void testMethod1(){
        
        integer totalAmount=100;
        string templatename='';
        
        
        opportunity opp= new opportunity();
        opp.name='test';
        opp.CloseDate=date.today();
        opp.StageName='qualification';
        opp.Account_Name__c='account test';
        opp.Project_Manager_list__c='manager list';
        opp.End_Date__c=date.today();
        opp.Total_Sales_Price__c='32';
        opp.Labour_Cost__c=56;
        opp.External_Cost__c=567;
        opp.Contingency_Cost__c=876;
        insert opp;
        
        Projects__c newEngagement = new Projects__c();
        newEngagement.Account__c = opp.Account_Name__c;
        newEngagement.Opportunityc__c = opp.Id;
        newEngagement.Name = opp.Name + ' - ' + System.today().format();
        newEngagement.Project_Manager__c=opp.Project_Manager_list__c;
        newEngagement.End_Date__c=opp.End_Date__c;
        newEngagement.Estimated_Project_Revenue__c = totalAmount;
        
        /* list<user> salesres=[select id,name,Sales_Responsible_Name__c,Email from user where Sales_Responsible_Name__c!=null and Sales_Responsible_Name__c=:newEngagement.Project_Manager__c];//'Anthony Kent'];//opp.Sales_Responsible__c];// id=:sales];
if(salesres.size()>0){

newEngagement.Sales_responsible_email__c=salesres[0].email;
}*/    
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
        
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id, 
                         Sales_Responsible_Name__c='manager list'
                        ); 
        insert uu;
        
        newEngagement.Total_Sales_Price__c=opp.Total_Sales_Price__c;
        newEngagement.Labour_Cost__c=opp.Labour_Cost__c;
        newEngagement.External_Cost__c=opp.External_Cost__c;
        newEngagement.Contingency_Cost__c=opp.Contingency_Cost__c;
        insert newEngagement;
        
        test.startTest();
        projectApex.disp();
        test.stopTest();
    } 
}

Apex class-I think need some changes in your apex

public class projectApex {
    public static PageReference disp(){
        integer totalAmount=100;
        string templatename='';
        
        
        opportunity opp= new opportunity();
        opp.name='test';
        opp.CloseDate=date.today();
        opp.StageName='qualification';
        opp.Account_Name__c='account test';
        opp.Project_Manager_list__c='manager list';
        opp.End_Date__c=date.today();
        insert opp;
        
       Projects__c newEngagement = new Projects__c(Account__c = opp.Account_Name__c, Opportunityc__c = opp.Id, Name = opp.Name + ' - ' + System.today().format(),Project_Manager__c=opp.Project_Manager_list__c,End_Date__c=opp.End_Date__c);
        insert newEngagement;
        
        list<Projects__c> upEngagement=[select id,name from Projects__c where id =: newEngagement.id];
        newEngagement.Estimated_Project_Revenue__c = totalAmount;
        for(Projects__c templist:upEngagement)
        {
            system.debug('templateIds'+templatename);
            templatename+=templist.Name+',';
        }
        templatename=templatename.removeEnd(',');
        newEngagement.Template_Project__c=templatename;
        
        
       list<user> salesres=[select id,name,Sales_Responsible_Name__c,Email from user where Sales_Responsible_Name__c!=null and Sales_Responsible_Name__c=:newEngagement.Project_Manager__c];//'Anthony Kent'];//opp.Sales_Responsible__c];// id=:sales];
        if(salesres.size()>0){
            
            
            newEngagement.Sales_responsible_email__c=salesres[0].email;
        }
        newEngagement.Total_Sales_Price__c=opp.Total_Sales_Price__c;
        newEngagement.Labour_Cost__c=opp.Labour_Cost__c;
        newEngagement.External_Cost__c=opp.External_Cost__c;
        newEngagement.Contingency_Cost__c=opp.Contingency_Cost__c;
        
        update newEngagement;
        PageReference pr = new PageReference('/' + newEngagement.Id);
        pr.setRedirect(true);
        return pr;
    }
}


If you find it is helpful please mark best answer.

Thanks