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
nasir jawednasir jawed 

Test method test coverage

Hi 

 

I want to write the test method for this Class and i am not able to cover the SOQL statements.The line marked in red are not covered and the test coverage is 42%.How to cover SOQL statements.The with "Underline" are uncovered.Please help

 

 
public class ProjectCreation {
    public Opportunity oppn;
    public String sel {get; set;}
    public Account accRecord;
    private ID Id;
    public Boolean isConverted {get; set;}
    
    public ProjectCreation(ApexPages.StandardController controller) {
        oppn = (Opportunity)controller.getRecord();
        Opportunity op = [Select id,Convert_to_Project__c from Opportunity where id=:oppn.id];
        id = op.id;
        if(op.Convert_to_Project__c)
            isConverted = true;
        else
            isConverted = false;
    }
            
    public List<SelectOption> getassociated(){
        List<SelectOption> opts = new List<SelectOption>();
        opts.add(new SelectOption('New Project','New Project'));
        opts.add(new SelectOption('Existing Project','Existing Project'));
        return opts;
    }
    
    public PageReference saveProject(){         
        PageReference oppPage; 

         if(sel=='New Project'){
            oppn = [select id,name,CloseDate,Project_name__c,Convert_to_Project__c,Counter_Party_Supplier__c,AccountId,Amount,Description from Opportunity where id=:oppn.Id];
            accRecord = [Select Id,Business_Line__c,Industry_Cluster__c,Product_Category__c,Subindustry_cluster__c,Application__c from Account where Id = :oppn.AccountId];
            
            Project__c newProj = new Project__c(Name=oppn.Name, 
                Dateline__c=oppn.CloseDate, Kickoff__c=System.Today(), Account_Name__c=oppn.AccountId,
                Supplier_Name__c= oppn.Counter_Party_Supplier__c, Revenue_Expected_Year1__c=oppn.Amount,
                Description__c = oppn.Description,Project_Progress__c='10%',Status__c='Active' );
            newProj.Related_To_Opportunity__c = oppn.Id;
            newProj.Business_Line__c = accRecord.Business_Line__c;
            newProj.Industry_Cluster__c = accRecord.Industry_Cluster__c;
            newProj.Product_Category__c = accRecord.Product_Category__c;
            newProj.Sub_Industry_Cluster__c = accRecord.Subindustry_cluster__c;
            newProj.Application__c = accRecord.Application__c;
            insert newProj;

            List<Opportunity_Product__c> OP = [Select Id,Product__c,Quantity__c,Sales_Price__c,Margin_1__c,CurrencyIsoCode,UOM__c from Opportunity_Product__c where Opportunity_Name__c =:oppn.Id];
            
            //System.Debug(' Start OP Size ' + OP.size());
            if(OP != null){
                for(Opportunity_Product__c opr : OP){
                    
                    Project_Product__c newPP = new Project_Product__c(Project__c=newProj.id,Product__c=opr.Product__c,
                        Quantity__c=opr.Quantity__c,Sales_Price__c=opr.Sales_Price__c,Margin__c=opr.Margin_1__c,
                        CurrencyIsoCode=opr.CurrencyIsoCode,UOM__c=opr.UOM__c);
                    insert newPP;
                    //System.Debug(' LPPP ' + newPP.id);
                }
            }
            //System.Debug(' End ' + OP.size());   
            oppn.Convert_to_Project__c = true;
            oppn.Project_Name__c = newProj.id;
            update oppn;
            oppPage = new ApexPages.StandardController(newProj).view();
            oppPage.setRedirect(true);
            return oppPage; 
        }else if(sel=='Existing Project' ){
            if(oppn.Project_name__c!=null){
            
                Project__c existingProj= [Select id, Name from Project__c where  id =:oppn.Project_name__c];    
                existingProj.Related_To_Opportunity__c = oppn.Id;
                
                update existingProj;
                List<Opportunity_Product__c> OP = [Select Id,Product__c,Quantity__c,Sales_Price__c,Margin_1__c,CurrencyIsoCode,UOM__c from Opportunity_Product__c where Opportunity_Name__c =:existingProj.Id];
                
                //System.Debug(' Start OP Size ' + OP.size());
                if(OP != null){
                    for(Opportunity_Product__c opr : OP){
                        
                        Project_Product__c newPP = new Project_Product__c(Project__c=existingProj.id,Product__c=opr.Product__c,
                            Quantity__c=opr.Quantity__c,Sales_Price__c=opr.Sales_Price__c,Margin__c=opr.Margin_1__c,
                            CurrencyIsoCode=opr.CurrencyIsoCode,UOM__c=opr.UOM__c);
                        insert newPP;
                        //System.Debug(' LPPP ' + newPP.id);
                    }
                }
            
                oppn.Convert_to_Project__c=true;
                update oppn;
                oppPage= new ApexPages.StandardController(existingProj).view();
                oppPage.setRedirect(true);
                return oppPage;
                
            }else{
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Please select an existing project!');
                ApexPages.addMessage(myMsg);
            }
        }  
        return null;        

    }
    
        public PageReference back(){  
            PageReference oppnPage = new ApexPages.StandardController(oppn).view();
            oppnPage.setRedirect(true);
            return oppnPage;
        }
        
        static testMethod void test1(){
         
         Opportunity opp = new Opportunity();
         Account ac = new Account();
         Project__c pro = new Project__c();
        
          
         opp.Name = 'test1';
         opp.Convert_to_Project__c=true;
         opp.CloseDate = System.Today();
         opp.StageName= 'Need Analysis';
         opp.CurrencyIsoCode ='CHF'; 
         opp.StageName= 'Need Analysis';        
         insert opp;
         
         
         pro.Name='test';
         pro.Status__c = 'Active';
         pro.Project_Progress__c='50%';
         pro.Kickoff__c=System.Today();
         pro.Dateline__c=System.Today();
         insert pro;
         
        
         
        
         
        
         
         ProjectCreation pc = new ProjectCreation(new ApexPages.StandardController(opp));
         pc.back();
         pc.saveProject();
Chamil MadusankaChamil Madusanka

Hi,

 

Add following statement

 

pc.oppn = opp;

 before,

 

ProjectCreation pc = new ProjectCreation(new ApexPages.StandardController(opp));
         pc.back();
         pc.saveProject();

 these statements.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

Chamil's Blog