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
PrabhataPrabhata 

test class

Hi Experts,

I need your help on test coverage for the class "AccountValidation" given below:
public class AccountValidation {

    public Account acc;
    Public String AccErr1 {get;set;}
    Public String AccErr4 {get;set;}
    Public String LoginErr {get;set;}
    Public String DomainName {get;set;}
    Public Id AccId {get;set;}
    Public Id ConId {get;set;}
    Public List<String> characters;
    public String input {get; set;}
    public String result {get; set;}
    public String char1,char2,char3,char4,char5,char6;
    Public Decimal CaptchaErr {get; set;} 
            
    public AccountValidation(ApexPages.StandardController controller) {
    
       if(ApexPages.currentPage().getParameters().get('id')!='' && ApexPages.currentPage().getParameters().get('id')!=null)
           AccId = ApexPages.currentPage().getParameters().get('id'); 
       DomainName = Label.Order_Page_Domain_Name;        
       acc = (Account)controller.getRecord(); 
       
       CaptchaErr = 0;
       characters = new List<String>{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0'};          
    
       ContactId(); 
    }
           
    public PageReference SaveAccount(){
                  
        if(acc.Name!='' && acc.Name!=null && acc.City__c!='' && acc.City__c!=null){

            if(input!='' && input!=null && input.length() == 6 && input.subString(0,1) == char1 && input.subString(1,2) == char2 && input.subString(2,3) == char3 && input.subString(3,4) == char4 && input.subString(4,5) == char5 && input.subString(5,6) == char6)
                CaptchaErr = 0; 
            else
                CaptchaErr = 1; 
                                
            list<Account> accounts = [Select Id, Email_Address__c from Account where Name=:acc.Name and City__c=:acc.City__c limit 1];
            
            list<Contact> contacts = new list<Contact>();
            if(acc.Email_Address__c!='' && acc.Email_Address__c!=null && accounts.size()>0)
                contacts = [Select Id from Contact where AccountId=:accounts[0].Id and City__c=:acc.City__c and Email_Address__c=:acc.Email_Address__c limit 1];

            if(contacts.size()>0 && CaptchaErr == 0){ 
                AccErr4 = Label.Order_First_Page_Error4; 
                AccErr1 = '';
            }         
            else if(accounts.size()>0 && CaptchaErr == 0){
                AccErr1 = Label.Order_First_Page_Error1; 
                AccErr4 = '';
            }           
            else{ 
                AccErr1 =  AccErr4 = '';   
                if(CaptchaErr == 0){        
                    insert acc;
        
                    List<Lead> TransformedLeadsList = new List<Lead>();
                    list<Lead> leads = [Select Transformed_Date__c from Lead where Company=:acc.Name and City__c=:acc.City__c];
                    if(leads.size()>0){
                        for(Lead ld : leads){
                            ld.Transformed_Date__c = (Date)system.today();
                            TransformedLeadsList.add(ld);
                        }
                        update TransformedLeadsList;
                    }
                                   
                    PageReference orderref = new PageReference(DomainName+'/AccCreation?id='+acc.Id);
                    orderref.setRedirect(true);
                    return orderref; 
                }
                return null;    
            }  
                                                        
        }
        return null;
    }
    
    public PageReference CreateContact(){
    
        if(acc.Name!='' && acc.Name!=null && acc.City__c!='' && acc.City__c!=null){
            
            list<Account> accounts = [Select Id from Account where Name=:acc.Name and City__c=:acc.City__c limit 1];
            final String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
            String randStr = '';
            while (randStr.length() < 6) {
               Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), 62);
               randStr += chars.substring(idx, idx+1);
            } 
                                
            Contact con = new Contact();
            con.FirstName = acc.First_Name__c;
            con.LastName = acc.Last_Name__c;
            con.AccountId = accounts[0].Id;
            con.Organisation_Type__c = acc.Organisation_Type__c;
            con.Road__c = acc.Road__c;
            con.Postal_Code__c = acc.Postal_Code__c;
            con.Function__c = acc.Function__c;
            con.Phone_Number__c = acc.Phone_Number__c;
            con.Email_Address__c = acc.Email_Address__c;
            con.Password__c = acc.First_Name__c+''+randStr;
            if(acc.Fax!=null && acc.Fax!='')
                con.Fax = acc.Fax;
            if(acc.Opening_Hours__c!=null && acc.Opening_Hours__c!='')    
                con.Opening_Hours__c = acc.Opening_Hours__c;
            insert con;              
            PageReference conref = new PageReference(DomainName+'/ContactCreation?id='+con.Id);
            conref.setRedirect(true);
            return conref;                                  
        }
        return null;
        
    }
    
    public void ContactId(){
    
        if(acc.Name!='' && acc.Name!=null && acc.City__c!='' && acc.City__c!=null && acc.Email_Address__c!='' && acc.Email_Address__c!=null){
            list<Contact> cons = [Select Id from Contact where AccountId=:acc.Id and City__c=:acc.City__c and Email_Address__c=:acc.Email_Address__c limit 1];
            if(cons.size()>0) ConId = cons[0].Id;
        }
        
    }
    
    public PageReference CheckAccount(){
        return null;
        
    }
    public Integer randomNumber(){
        Integer random = Math.Round(Math.Random() * characters.Size());
        if(random == characters.size()){
            random--;
        }
        return random;
    }
    public String getChar1(){
        char1 = characters[randomNumber()];
        return char1;
    }
    public String getChar2(){
        char2 = characters[randomNumber()];
        return char2;
    }
    public String getChar3(){
        char3 = characters[randomNumber()];
        return char3;
    }
    public String getChar4(){
        char4 = characters[randomNumber()];
        return char4;
    }
    public String getChar5(){
        char5 = characters[randomNumber()];
        return char5;
    }
    public String getChar6(){
        char6 = characters[randomNumber()];
        return char6;
    }
        
}
and the test class which I have started writing but unable to get the control inside CreateContact(). Given below is my test code
@isTest
private class Test_AccountValidation{
    static testMethod void runTest_AccountValidation(){
        PageReference prf = Page.AccLogin;
        Test.setCurrentPage(prf);
     //   Account acc = new Account(Name = 'Test Account', City__c = 'Test_City', Password__c = 'TestPassword'); 
     //   AccountValidation accVal = new AccountValidation(new ApexPages.StandardController(acc));        
        Account acnt = new Account(Name = 'Test Account', First_Name__c = 'Test FirstName', Last_Name__c = 'Test LastName', Organisation_Type__c = 'Association', Road__c = 'Test Road', Postal_Code__c = '12345', City__c = 'Test City', Function__c = 'other', Phone_Number__c = '1234567890', Email_Address__c = 'Test@testmail.com', Fax = '1234567890', Number_of_Employees__c = 'Between 100 and 500', Opening_Hours__c = 'Monday Morning');
        ApexPages.currentPage().getParameters().get('id'); 
        AccountValidation controller = new AccountValidation(new ApexPages.StandardController(acnt));
        controller.SaveAccount();
        insert acnt;
        
        
     /*   AccountValidation controller1 = new AccountValidation(new ApexPages.StandardController(acnt));
        
             
            Contact con = new Contact(FirstName = 'Test FirstName', LastName = 'Test LastName', Password__c = 'Testcon123', Organisation_Type__c = 'Association', Road__c = 'Test Road', Postal_Code__c = '12345', Function__c = 'other', Phone_Number__c = '1234567890', Email_Address__c = 'Test@testmail.com', Fax = '1234567890', Opening_Hours__c = 'Monday Morning');
        insert con;
        controller1.CreateContact(); */
        
    }
}
While I uncomment every line in the test class and run it following error is showing as below:
User-added image
Kindly help me out in this.

 
MithunPMithunP
Hi Prabhata,

You no need to insert contact in your test class, I have updated your test class like below try this.
 
@isTest
private class Test_AccountValidation{
    static testMethod void runTest_AccountValidation(){
        PageReference prf = Page.AccLogin;
        Test.setCurrentPage(prf);
     //   Account acc = new Account(Name = 'Test Account', City__c = 'Test_City', Password__c = 'TestPassword'); 
     //   AccountValidation accVal = new AccountValidation(new ApexPages.StandardController(acc));        
        Account acnt = new Account(Name = 'Test Account', First_Name__c = 'Test FirstName', Last_Name__c = 'Test LastName', Organisation_Type__c = 'Association', Road__c = 'Test Road', Postal_Code__c = '12345', City__c = 'Test City', Function__c = 'other', Phone_Number__c = '1234567890', Email_Address__c = 'Test@testmail.com', Fax = '1234567890', Number_of_Employees__c = 'Between 100 and 500', Opening_Hours__c = 'Monday Morning');
        ApexPages.currentPage().getParameters().get('id'); 
        AccountValidation controller = new AccountValidation(new ApexPages.StandardController(acnt));
        controller.SaveAccount();
        insert acnt;
        
        
       AccountValidation controller1 = new AccountValidation(new ApexPages.StandardController(acnt));
        
             
           /* Contact con = new Contact(FirstName = 'Test FirstName', LastName = 'Test LastName', Password__c = 'Testcon123', Organisation_Type__c = 'Association', Road__c = 'Test Road', Postal_Code__c = '12345', Function__c = 'other', Phone_Number__c = '1234567890', Email_Address__c = 'Test@testmail.com', Fax = '1234567890', Opening_Hours__c = 'Monday Morning');
        insert con;*/
        controller1.CreateContact(); 
        
    }
}
Best Regards,
Mithun.
 
PrabhataPrabhata
Hi Mithun,

Thanks a lot for your answer. It helped me a bit to take test covarage up to 54% but after run it is throwing an error as given below:User-added image

I guess the Account Id required from the pagereference for creating Contact is not getting associated properly. Also a part in the code remained untouched as shown below:
User-added image
MithunPMithunP
Hi Prabhata,

Can you verify is there any validation rules/triggers on Contact and Account.

Best Regards,
Mithun.