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
Azeemuddin FirasatAzeemuddin Firasat 

Test class code coverage is 40% got, I am new of writing test class could any body help ?

Apex Class:

public class Testcontroller {
    public String temp {get; set;}
    public Opportunity oppstagename{set;get;}
    public account accrating{set;get;}
    public Lead ldsource{set;get;}
    public List<Opportunity> opplist{set;get;}
    public List<Account> acclist{Set;get;}
    public List<Lead> ldlist{set;get;}
    public boolean oppstgbool{set;get;}
    public boolean accbool{set;get;}
    public boolean ldbool{set;get;}
    public Opportunity Oppt{set;get;}
    public boolean newoppt{set;get;}
    
    public List<Contact> conList {get;set;}
    public List<Contact> conAddList {get;set;}
    public String conName {get;set;}   
    public boolean addrowbool{set;get;}
    public Integer rowNum{get;set;}
    
    public Testcontroller() 
    {
        oppstagename=new Opportunity();
        accrating=new account();
        ldsource=new lead();
        oppstgbool=false;
        accbool=false;
        ldbool=false;
        newoppt=false;
        addrowbool=false;
        Oppt=new Opportunity();       
    } 
    
    public PageReference newopptsave()
    {
        insert oppt;
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Confirm,'New Opportunity is Sucessfully Saved.'));
        return null;
    }
    
    public PageReference newopptsavenew()
    {
        insert oppt;
        oppt.clear();
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Confirm,'New Opportunity is Sucessfully Saved and Cleared.'));
        return null;
    }
    
    public SelectOption[] getAccountNames(){
        SelectOption[] acccopptoptions = new SelectOption[]{};
            acccopptoptions.add(new SelectOption('','--None--')); 
        
        for (Opportunity accoppt : [select id, name,accountid,account.name FROM Opportunity ]) {  
            if(accoppt.account.name!= null) {
                acccopptoptions.add(new SelectOption(accoppt.accountid, accoppt.account.name));  
            }
        } 
        return acccopptoptions;
    }
    
    public void find()
    {
        if(temp == 'Opportunity')
        {
            oppstgbool=true;
            accbool=false;
            ldbool=false;
            newoppt=false;
            addrowbool=false;
            opplist=[Select id,Name,StageName FROM Opportunity WHERE StageName=:oppstagename.StageName Limit 5];
        }
        else if(temp == 'Account')
        {
            oppstgbool=false;
            accbool=true;
            ldbool=false;
            newoppt=false;
            addrowbool=false;
            acclist=[Select id,Name,Rating FROM Account WHERE Rating=:accrating.Rating and Rating!=null Limit 5];
        } 
        else if(temp == 'Lead')
        {
            oppstgbool=false;
            accbool=false;
            ldbool=true;
            newoppt=false;
            addrowbool=false;
            ldlist=[Select id,Name,LeadSource FROM Lead WHERE LeadSource=:ldsource.LeadSource and LeadSource!=null Limit 5];
        }
        else if(temp == 'New Opportunity')
        {
            oppstgbool=false;
            accbool=false;
            ldbool=false;
            newoppt=true;
            addrowbool=false;
            Oppt=new Opportunity();
        }
        else if(temp == 'Addrow Contact')
        {
            oppstgbool=false;
            accbool=false;
            ldbool=false;
            newoppt=false;
            addrowbool=True;
            String sql='SELECT id,LastName,Phone,Email FROM Contact';
            conlist=Database.query(sql);
            conAddList=new List<Contact>();
            conAddList.add(new Contact());
        }
        else{
            oppstgbool=false;
            accbool=false;
            ldbool=false;
            addrowbool=false;
            newoppt=false;
        }
    }   
    
    public void AddRow(){
        conAddList.add(new Contact());
    }    
    
    public void delRow()
    {
        rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
        conAddList.remove(rowNum);   
    }   
    
    public void Savecon()
    {
        insert conAddList;
    }
    
    public void Cancel()
    {
        oppstgbool=false;
        accbool=false;
        ldbool=false;
        addrowbool=false;
    }
}
 
Test Class:

@isTest
public class Testcontroller_Test {
    static testMethod void Testcontroller_Test() {
        
        Account acc = new Account(Name='Abce');
        insert acc;
        Opportunity  testOppty = new Opportunity();
        testOppty.name='testOppty';
        testOppty.AccountId=acc.id;
        testOppty.StageName='Open';
        testOppty.CloseDate=System.today();
        insert testOppty;
        
        Testcontroller tc=new Testcontroller();
        tc.find();
        tc.getAccountNames();
        tc.newopptsave();
        tc.newopptsavenew();
        tc.AddRow();
        tc.Savecon();
        tc.Cancel();
    }
}

 
PavanKPavanK
Hi ,

Below code may not cover 100% coverage but it will increase coverage significantly
 
@isTest
public class Testcontroller_Test {
    static testMethod void Testcontroller_Test() {
        
        Account acc = new Account(Name='Abce');
        insert acc;
        Opportunity  testOppty = new Opportunity();
        testOppty.name='testOppty';
        testOppty.AccountId=acc.id;
        testOppty.StageName='Open';
        testOppty.CloseDate=System.today();
        insert testOppty;
        
        Testcontroller tc=new Testcontroller();
        tc.find();
tc.temp='Opportunity';
tc.find();
tc.temp='Account';
tc.find();
tc.temp='New Opportunity';
tc.find();
tc.temp='Addrow Contact';
tc.find();

        tc.getAccountNames();
        tc.newopptsave();
        tc.newopptsavenew();
        tc.AddRow();
        tc.Savecon();
        tc.Cancel();
    }
}