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
bonny mankotiabonny mankotia 

Need a Test Class For This Simple code

Here is the Code.

public class actionFunctionController {
    public Account acc{get;set;}
    public Boolean showPhone{get;set;}
 
    public actionFunctionController(){
        acc = new Account();
        showPhone = false;
    }
 
    public PageReference priorityChanged(){
        if(acc.CustomerPriority__c == 'High'){
            showPhone = true;
        }
        else
        {
            showPhone = false;
        }
        return null;
    }
}
Best Answer chosen by bonny mankotia
AshlekhAshlekh
Hi Bonny,

Here is the documentation for How to write test class.

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm
 
@isTest
public class ActionFunctionControllerTest{

public static TestMethod void UnitTestOne(){
		
		Test.startTest();
		actionFunctionController  cntrl = new actionFunctionController();
		system.assertEquals(cntrl.showPhone , false);
		cntr.acc.CustomerPriority__c = 'High';
		cntr.priorityChanged();
		system.assertEquals(cntrl.showPhone , true);
		Test.stopTest();
		
}

public static TestMethod void UnitTestTwo(){
		
		Test.startTest();
		actionFunctionController  cntrl = new actionFunctionController();
		system.assertEquals(cntrl.showPhone , false);
		cntr.acc.CustomerPriority__c = 'High1'; 
		cntr.priorityChanged();
		system.assertEquals(cntrl.showPhone , false);
		Test.stopTest();
		
		
}

}

Mark this as a solution if it helps you. ENJOY APEX

-Thanks
Ashlekh Gera

All Answers

AshlekhAshlekh
Hi Bonny,

Here is the documentation for How to write test class.

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm
 
@isTest
public class ActionFunctionControllerTest{

public static TestMethod void UnitTestOne(){
		
		Test.startTest();
		actionFunctionController  cntrl = new actionFunctionController();
		system.assertEquals(cntrl.showPhone , false);
		cntr.acc.CustomerPriority__c = 'High';
		cntr.priorityChanged();
		system.assertEquals(cntrl.showPhone , true);
		Test.stopTest();
		
}

public static TestMethod void UnitTestTwo(){
		
		Test.startTest();
		actionFunctionController  cntrl = new actionFunctionController();
		system.assertEquals(cntrl.showPhone , false);
		cntr.acc.CustomerPriority__c = 'High1'; 
		cntr.priorityChanged();
		system.assertEquals(cntrl.showPhone , false);
		Test.stopTest();
		
		
}

}

Mark this as a solution if it helps you. ENJOY APEX

-Thanks
Ashlekh Gera
This was selected as the best answer
Rupal KumarRupal Kumar
hi bonny mankotia,
This is the test class for above class .
@isTest
public class actionFunctionControllerTest{
    public static testmethod void myTest()
    {
     
          Account a= new Account();
          
              a.Name='Test LName';
             a.CustomerPriority__c='test' ;

          insert a;
        
        actionFunctionController cl = new actionFunctionController();
        cl.priorityChanged();
       
       
  }  
}


Thanks
Rupal Kumar
Mirketa Software Pvt Ltd
http://mirketa.com/index.html
bonny mankotiabonny mankotia
Thanks Its working for me.But please change CNTR to CNTRL in line 9,10,21,22.
bonny mankotiabonny mankotia
Hello Rupal thanks but you Test code not cover IF statement Block.
Rupal KumarRupal Kumar
hi bonny mankotia,
 This code covers 90% of the block(code coverage) so we need not cover If block statement as salesforce test clas requres only75% of code coverage for deployment.


Thanks
Rupal Kumar