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
HoysalaHoysala 

how to do 85% code coverage for the below code? please help, thanks in advance

public with sharing class AccController
{
    public list <account> acc {get;set;} 
    public string searchstring {get;set;} 
    Public Boolean ShowCreateBlock{get;set;}
    public Boolean ShowBlock{get;set;}
    Public Boolean DisplaySearchResults{get;set;}
    public Account account{get;set;}
    Public Account NewAccountRecord{get;set;}
    
    public AccController()
    {
        ShowCreateBlock = false;
        ShowBlock=True;
        DisplaySearchResults = false;
        NewAccountRecord= New Account();
         
    }

    public String getAccount()
    {
        return null;
    }


    public String options { get; set; }

    public PageReference executeSearch() {
        return null;
    }
    public list<account> showList = new list<account>([select Name, Email__c,Phone from Account]);
    Public list<account> getshowList()
    {
        
        return showList;
    }
   
  
    public PageReference create()
    {
        return null;
    }

    public pagereference search()
    { 
        string searchquery='select name,id from account where name like \'%'+searchstring+'%\' Limit 20'; 
        acc= Database.query(searchquery);
        DisplaySearchResults =true;
        if(acc.size()==0)
        {
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'account not found'));
          ShowCreateBlock = true; 
          ShowBlock=False;
        }
        return null;
    }
    public void clear()
    { 
    acc.clear(); 
    } 
    Public pagereference saveRecord(){
        insert NewAccountRecord;
        pagereference  pageRef = new pagereference('/'+NewAccountRecord.Id);
        return pageRef ;
    }
}

 
Best Answer chosen by Hoysala
Raj VakatiRaj Vakati
You controller
 
public with sharing class AccController
{
    public list <account> acc {get;set;} 
    public string searchstring {get;set;} 
    Public Boolean ShowCreateBlock{get;set;}
    public Boolean ShowBlock{get;set;}
    Public Boolean DisplaySearchResults{get;set;}
    public Account account{get;set;}
    Public Account NewAccountRecord{get;set;}
    
    public AccController()
    {
        ShowCreateBlock = false;
        ShowBlock=True;
        DisplaySearchResults = false;
        NewAccountRecord= New Account(Name='Test');
        
    }
    
    public String getAccount()
    {
        return null;
    }
    
    
    public String options { get; set; }
    
    public PageReference executeSearch() {
        return null;
    }
    public list<account> showList = new list<account>([select Name, Email__c,Phone from Account]);
    Public list<account> getshowList()
    {
        
        return showList;
    }
    
    
    public PageReference create()
    {
        return null;
    }
    
    public pagereference search()
    { 
        string searchquery='select name,id from account where name like \'%'+searchstring+'%\' Limit 20'; 
        acc= Database.query(searchquery);
        DisplaySearchResults =true;
        if(acc.size()==0)
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'account not found'));
            ShowCreateBlock = true; 
            ShowBlock=False;
        }
        return null;
    }
    public void clear()
    { 
        acc.clear(); 
    } 
    Public pagereference saveRecord(){
        insert NewAccountRecord;
        pagereference  pageRef = new pagereference('/'+NewAccountRecord.Id);
        return pageRef ;
    }
}



And test class
 
@isTest 
private class AccControllerTest{
    
    
    static testMethod void testMethodPOST(){
        List<Account> accList= new List<Account>();
        for(Integer i= 0 ; i<10 ; i++){
            accList.add(new Account(Name = 'test Account' ,Email__c='test@gmail.com'));
        }
        insert accList;
        
        AccController listCon = new AccController();
        listCon.account = accList[0];
        listCon.options='--';
        listCon.getshowList();
        listCon.executeSearch();
        listCon.getAccount();
        listCon.create();
        listCon.search();
        listCon.clear();
        listCon.saveRecord();
        
        
        
        
        
        
    }
    
}

 

All Answers

Raj VakatiRaj Vakati
You controller
 
public with sharing class AccController
{
    public list <account> acc {get;set;} 
    public string searchstring {get;set;} 
    Public Boolean ShowCreateBlock{get;set;}
    public Boolean ShowBlock{get;set;}
    Public Boolean DisplaySearchResults{get;set;}
    public Account account{get;set;}
    Public Account NewAccountRecord{get;set;}
    
    public AccController()
    {
        ShowCreateBlock = false;
        ShowBlock=True;
        DisplaySearchResults = false;
        NewAccountRecord= New Account(Name='Test');
        
    }
    
    public String getAccount()
    {
        return null;
    }
    
    
    public String options { get; set; }
    
    public PageReference executeSearch() {
        return null;
    }
    public list<account> showList = new list<account>([select Name, Email__c,Phone from Account]);
    Public list<account> getshowList()
    {
        
        return showList;
    }
    
    
    public PageReference create()
    {
        return null;
    }
    
    public pagereference search()
    { 
        string searchquery='select name,id from account where name like \'%'+searchstring+'%\' Limit 20'; 
        acc= Database.query(searchquery);
        DisplaySearchResults =true;
        if(acc.size()==0)
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'account not found'));
            ShowCreateBlock = true; 
            ShowBlock=False;
        }
        return null;
    }
    public void clear()
    { 
        acc.clear(); 
    } 
    Public pagereference saveRecord(){
        insert NewAccountRecord;
        pagereference  pageRef = new pagereference('/'+NewAccountRecord.Id);
        return pageRef ;
    }
}



And test class
 
@isTest 
private class AccControllerTest{
    
    
    static testMethod void testMethodPOST(){
        List<Account> accList= new List<Account>();
        for(Integer i= 0 ; i<10 ; i++){
            accList.add(new Account(Name = 'test Account' ,Email__c='test@gmail.com'));
        }
        insert accList;
        
        AccController listCon = new AccController();
        listCon.account = accList[0];
        listCon.options='--';
        listCon.getshowList();
        listCon.executeSearch();
        listCon.getAccount();
        listCon.create();
        listCon.search();
        listCon.clear();
        listCon.saveRecord();
        
        
        
        
        
        
    }
    
}

 
This was selected as the best answer
Raj VakatiRaj Vakati
Its 100 % code coverage 
HoysalaHoysala
@ Raj Vakati its showing 00% code coverage, any other code u can help with?
Raj VakatiRaj Vakati
Check you test class result .. it might be failing due to some reason 
HoysalaHoysala
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountTrigger: execution of AfterInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.AccountTrigger: line 13, column 1: []
Raj VakatiRaj Vakati
Check why its failing in account trigger .. 

If you have account trigger test class insert the same data into our test class
HoysalaHoysala
please can u show where to insert it? i am totally new in salesforce, am not able to write thecode coverage since 2 days
 
HoysalaHoysala
I have shared the trigger above, please resolve it for me.... thanks in advance
 
Raj VakatiRaj Vakati
try this
 
@isTest 
private class AccControllerTest{
    
    
    static testMethod void testMethodPOST(){
        List<Account> accList= new List<Account>();
        for(Integer i= 0 ; i<10 ; i++){
            accList.add(new Account(Name = 'test Account' ,Email__c='test@gmail.com' ,Phone='123123'));
        }
        insert accList;
        
        AccController listCon = new AccController();
        listCon.account = accList[0];
        listCon.options='--';
        listCon.getshowList();
        listCon.executeSearch();
        listCon.getAccount();
        listCon.create();
        listCon.search();
        listCon.clear();
        listCon.saveRecord();
        
        
        
        
        
        
    }
    
}

 
Raj VakatiRaj Vakati
its worked?
HoysalaHoysala
Class.AccControllerTest.testMethodPOST: line 10, column 1.... this was error
 
HoysalaHoysala
@ Raj vakati, any solution?