+ Start a Discussion
Yelper DevYelper Dev 

Help with a test class on VF Page (Extension)

Hello,

Need some help in creating test class for the following extension bellow. I am using an Account standardcontroller.
 
public class GetContactFromAccount {
    
    public Contact contact {get;set;}
    public Account account {get;set;}
    private final ApexPages.StandardController controller;
    
    public getContactFromAccount(ApexPages.StandardController controller){
        this.controller = controller;
        account = (Account)controller.getRecord();
        contact = new Contact();
    }    
    
    public PageReference save(){
        insert account;
        contact.MailingStreet = account.BillingStreet;
        contact.MailingCity = account.BillingCity;
        contact.MailingState = account.BillingState;
        contact.MailingPostalCode = account.BillingPostalCode;
        contact.MailingState = account.BillingCountry;        
        contact.AccountId = account.Id;
        contact.Type__c = 'Subscriber';
        insert contact;
        Pagereference pageRef = New PageReference('/'+account.Id);
        System.debug(contact);
        return pageRef;
    }
    
}

Thank you!
Best Answer chosen by Yelper Dev
Yelper DevYelper Dev
Hi raj tried another approach, made 2 test methods to check if there are fields has not been filled-out and other one data has been saved.
@isTest
public class GetContactFromAccountTest {  

    @isTest static void verifyAcctSaveTest(){
        PageReference pageRef = Page.CreateAccountWithContact;
        Test.setCurrentPage(pageRef);
        
        //Set account the standard controller
        Account acct = new Account();
        Contact con = new Contact();
        ApexPages.StandardController acctController = new ApexPages.StandardController(acct);
        
        //setup extension and pass acctController
        getContactFromAccount accountContactExtension = new getContactFromAccount(acctController);
        
        Test.startTest(); 
        accountContactExtension.account.Name = 'Test Account';
        accountContactExtension.account.BillingStreet = '1931  Lake Road';
        accountContactExtension.account.BillingCity = 'Maple Shade';
        accountContactExtension.account.BillingState = 'New Jersey';
        accountContactExtension.account.BillingPostalCode = '08052';
        accountContactExtension.account.BillingCountry = 'US';
         accountContactExtension.contact.LastName = 'Dawg';
        accountContactExtension.contact.Email = 'fritzie.tongo@gmail.com';
        accountContactExtension.save();  
        Test.stopTest(); 
        Account verifyAcct = [SELECT Id from Account WHERE Id =: accountContactExtension.account.Id];
        //Validate if data was saved successfully.
        System.assert(verifyAcct.Id !=NULL);
    }
    @isTest static void errorHandlingAcctSaveTest(){
        PageReference pageRef = Page.CreateAccountWithContact;
        Test.setCurrentPage(pageRef);
        
        //Set account the standard controller
        Account acct = new Account();
        Contact con = new Contact();
        ApexPages.StandardController acctController = new ApexPages.StandardController(acct);
        
        //setup extension and pass acctController
        getContactFromAccount accountContactExtension = new getContactFromAccount(acctController);
        
        Test.startTest(); 
        accountContactExtension.account.Name = Label.LBL_No_Account_Name;
        accountContactExtension.account.BillingStreet = '1931  Lake Road';
        accountContactExtension.account.BillingCity = 'Maple Shade';
        accountContactExtension.account.BillingState = 'New Jersey';
        accountContactExtension.account.BillingPostalCode = '08052';
        accountContactExtension.account.BillingCountry = 'US';
         accountContactExtension.contact.LastName = 'Dawg';
        accountContactExtension.contact.Email = 'fritzie.tongo@gmail.com';
        accountContactExtension.save();  
        Test.stopTest(); 
        //Loop through messages to check if there are required fields that does not have any value
        List<Apexpages.Message> msgs = ApexPages.getMessages();
        boolean verifyMsg = false;
        for(ApexPages.Message err : msgs){
            if(err.getDetail().contains(Label.LBL_No_Account_Name)){
                verifyMsg = true;
            }
        }
        system.assert(verifyMsg);
    }
}

 

All Answers

Raj VakatiRaj Vakati
Use this .. Replace your page name with YOURPAGENAME in code
@isTest 
public class GetContactFromAccountTest 
{
	static testMethod void testMethod1() 
	{
	
		
		Account testAccount = new Account();//Insert Account
        testAccount.Name='Test Account' ;
         testAccount.BillingStreet =  '24 Willie Mays Plaza';
  testAccount.BillingCity= 'San Francisco';
  testAccount.BillingState= 'California';
  testAccount.BillingPostalCode ='94017';
  testAccount.BillingCountry ='USA';
  
 		insert testAccount;
		
        Contact cont = new Contact();
        cont.FirstName ='Test';
        cont.LastName ='Test';
        cont.accountid =testAccount.id;
        insert cont;
		Test.StartTest(); 
		PageReference  pref = Page.YOURPAGENAME; 
		pref.getParameters().put('id',testAccount.id);
		Test.setCurrentPage(pref);

			ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
			GetContactFromAccount  accCon = new GetContactFromAccount (sc);
			accCon.save();
			
		Test.StopTest();
		
	}
}

 
Raj VakatiRaj Vakati
Your code is not correct .. because you are trying to insert the account again  .. use this code and its test class 




 
public class GetContactFromAccount {
    
    public Contact contact {get;set;}
    public Account account {get;set;}
    private final ApexPages.StandardController controller;
    
    public getContactFromAccount(ApexPages.StandardController controller){
        this.controller = controller;
        account = (Account)controller.getRecord();
        contact = new Contact();
    }    
    
    public PageReference save(){
     //   insert account;
        contact.MailingStreet = account.BillingStreet;
        contact.MailingCity = account.BillingCity;
        contact.MailingState = account.BillingState;
        contact.MailingPostalCode = account.BillingPostalCode;
        contact.MailingState = account.BillingCountry;        
        contact.AccountId = account.Id;
        contact.LastName =account.Name;
        contact.Type__c = 'Subscriber';
        insert contact;
        Pagereference pageRef = New PageReference('/'+account.Id);
        System.debug(contact);
        return pageRef;
    }
    
}



 
@isTest 
public class GetContactFromAccountTest 
{
    static testMethod void testMethod1() 
    {
        
        
        Account testAccount = new Account();//Insert Account
        testAccount.Name='Test Account' ;
        testAccount.BillingStreet =  '24 Willie Mays Plaza';
        testAccount.BillingCity= 'San Francisco';
        testAccount.BillingState= 'California';
        testAccount.BillingPostalCode ='94017';
        testAccount.BillingCountry ='USA';
        
        insert testAccount;
        
        Contact cont = new Contact();
        cont.FirstName ='Test';
        cont.LastName ='Test';
        cont.accountid =testAccount.id;
        insert cont;
        Test.StartTest(); 
        
        ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
        GetContactFromAccount  accCon = new GetContactFromAccount (sc);
        accCon.save();
        
        Test.StopTest();
        
    }
}

 
Raj VakatiRaj Vakati
you can check asserting as below 

https://help.salesforce.com/articleView?id=000002839&type=1​​​​​​​
 
@isTest 
public class GetContactFromAccountTest 
{
    static testMethod void testMethod1() 
    {
        
        
        Account testAccount = new Account();//Insert Account
        testAccount.Name='Test Account' ;
        testAccount.BillingStreet =  '24 Willie Mays Plaza';
        testAccount.BillingCity= 'San Francisco';
        testAccount.BillingState= 'California';
        testAccount.BillingPostalCode ='94017';
        testAccount.BillingCountry ='USA';
        
        insert testAccount;
        
        Contact cont = new Contact();
        cont.FirstName ='Test';
        cont.LastName ='Test';
        cont.accountid =testAccount.id;
        insert cont;
        Test.StartTest(); 
        
        ApexPages.StandardController sc = new ApexPages.StandardController(testAccount);
        GetContactFromAccount  accCon = new GetContactFromAccount (sc);
        accCon.save();
        System.assertEquals('San Francisco' , [ Select Id ,MailingCity   from Contact where MailingCity  !=NULL ].MailingCity  );
        Test.StopTest();
        
    }
}

 
Raj VakatiRaj Vakati
Do you need any other info ?
Yelper DevYelper Dev
Hi raj tried another approach, made 2 test methods to check if there are fields has not been filled-out and other one data has been saved.
@isTest
public class GetContactFromAccountTest {  

    @isTest static void verifyAcctSaveTest(){
        PageReference pageRef = Page.CreateAccountWithContact;
        Test.setCurrentPage(pageRef);
        
        //Set account the standard controller
        Account acct = new Account();
        Contact con = new Contact();
        ApexPages.StandardController acctController = new ApexPages.StandardController(acct);
        
        //setup extension and pass acctController
        getContactFromAccount accountContactExtension = new getContactFromAccount(acctController);
        
        Test.startTest(); 
        accountContactExtension.account.Name = 'Test Account';
        accountContactExtension.account.BillingStreet = '1931  Lake Road';
        accountContactExtension.account.BillingCity = 'Maple Shade';
        accountContactExtension.account.BillingState = 'New Jersey';
        accountContactExtension.account.BillingPostalCode = '08052';
        accountContactExtension.account.BillingCountry = 'US';
         accountContactExtension.contact.LastName = 'Dawg';
        accountContactExtension.contact.Email = 'fritzie.tongo@gmail.com';
        accountContactExtension.save();  
        Test.stopTest(); 
        Account verifyAcct = [SELECT Id from Account WHERE Id =: accountContactExtension.account.Id];
        //Validate if data was saved successfully.
        System.assert(verifyAcct.Id !=NULL);
    }
    @isTest static void errorHandlingAcctSaveTest(){
        PageReference pageRef = Page.CreateAccountWithContact;
        Test.setCurrentPage(pageRef);
        
        //Set account the standard controller
        Account acct = new Account();
        Contact con = new Contact();
        ApexPages.StandardController acctController = new ApexPages.StandardController(acct);
        
        //setup extension and pass acctController
        getContactFromAccount accountContactExtension = new getContactFromAccount(acctController);
        
        Test.startTest(); 
        accountContactExtension.account.Name = Label.LBL_No_Account_Name;
        accountContactExtension.account.BillingStreet = '1931  Lake Road';
        accountContactExtension.account.BillingCity = 'Maple Shade';
        accountContactExtension.account.BillingState = 'New Jersey';
        accountContactExtension.account.BillingPostalCode = '08052';
        accountContactExtension.account.BillingCountry = 'US';
         accountContactExtension.contact.LastName = 'Dawg';
        accountContactExtension.contact.Email = 'fritzie.tongo@gmail.com';
        accountContactExtension.save();  
        Test.stopTest(); 
        //Loop through messages to check if there are required fields that does not have any value
        List<Apexpages.Message> msgs = ApexPages.getMessages();
        boolean verifyMsg = false;
        for(ApexPages.Message err : msgs){
            if(err.getDetail().contains(Label.LBL_No_Account_Name)){
                verifyMsg = true;
            }
        }
        system.assert(verifyMsg);
    }
}

 
This was selected as the best answer
JRP SCJRP SC
Download FIFA 20 For PC (https://www.bajaquest.com/fifa-20-download-pc/) That too for free only from BajaQuest.com