+ Start a Discussion
mac adminmac admin 

Test class for the extenstion class

Hi all,
Can anyone help in writting the test class for the below extension class.

public class gdaControllerExtension {

    Public final GFrm__c gfrm;

    public gdaControllerExtension (ApexPages.StandardController stdController) {
        this.gfrm = (GFrm__c)stdController.getRecord();
    }

    
  public void mfarten()
  {

 
  if(gfrm.SelectProgram__c=='MA')
  {
      gfrm.Select_One_Art_Ed__c= null;
        
  }else if(gform.SelectProgram__c=='AED'){
 
      gfrm.SelectOne_MFA__c= null;

   
  }else if(gfrm.SelectProgram__c=='enroll'){
 
      gform.SelectOne_MFA__c= null;

 
  }
 
  }

}

Thanks in advace.

Regards,
mac.
Best Answer chosen by mac admin
Rohit B ☁Rohit B ☁
Here is the updated test class
@isTest
private class gdaControllerExtensionTest {
	@testSetup		//Method to create test data for this class
	static void createTestData() {
		List<GFrm__c> lstGF = new List<GFrm__c>();
		
		GFrm__c gf = new GFrm__c();
		gf.Name = 'Test';
		gf.Select_One_Art_Ed__c = 'Test Value Art';
		gf.SelectOne_MFA__c = 'Test Value MFA';
		gf.SelectProgram__c = 'MA';
		lstGF.add(gf);
		
		gf = new GFrm__c();
		gf.Name = 'Test';
		gf.Select_One_Art_Ed__c = 'Test Value Art';
		gf.SelectOne_MFA__c = 'Test Value MFA';
		gf.SelectProgram__c = 'AED';
		lstGF.add(gf);
		
		gf = new GFrm__c();
		gf.Name = 'Test';
		gf.Select_One_Art_Ed__c = 'Test Value Art';
		gf.SelectOne_MFA__c = 'Test Value MFA';
		gf.SelectProgram__c = 'enroll';
		lstGF.add(gf);
		
		insert lstGF;
	} // END static void createTestData() {
	
	@isTest
	static void testMethod_MA() {
		GFrm__c gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c 
						WHERE SelectProgram__c = 'MA' LIMIT 1][0];
		Test.startTest();
			ApexPages.StandardController stdCont = new ApexPages.StandardController(gf);
			gdaControllerExtension gdaCE = new gdaControllerExtension(stdCont);
			gdaCE.mfarten();
		Test.stopTest();
		gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c 
						WHERE SelectProgram__c = 'MA' LIMIT 1][0];
		System.assertEquals(gf.Select_One_Art_Ed__c, null);
	} // END static void testMethod_MA() {
	
	@isTest
	static void testMethod_AED() {
		GFrm__c gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c 
						WHERE SelectProgram__c = 'AED' LIMIT 1][0];
		Test.startTest();
			ApexPages.StandardController stdCont = new ApexPages.StandardController(gf);
			gdaControllerExtension gdaCE = new gdaControllerExtension(stdCont);
			gdaCE.mfarten();
		Test.stopTest();
		gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c 
						WHERE SelectProgram__c = 'AED' LIMIT 1][0];
		System.assertEquals(gf.SelectOne_MFA__c, null);
	} // END static void testMethod_AED() {
	
	@isTest
	static void testMethod_Enroll() {
		GFrm__c gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c 
						WHERE SelectProgram__c = 'enroll' LIMIT 1][0];
		Test.startTest();
			ApexPages.StandardController stdCont = new ApexPages.StandardController(gf);
			gdaControllerExtension gdaCE = new gdaControllerExtension(stdCont);
			gdaCE.mfarten();
		Test.stopTest();
		gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c 
						WHERE SelectProgram__c = 'enroll' LIMIT 1][0];
		System.assertEquals(gf.SelectOne_MFA__c, null);
	} // END static void testMethod_Enroll() {
} // END private class gdaControllerExtensionTest {

Try this.

All Answers

Rohit B ☁Rohit B ☁
Hi Mac,
Refer the below code:-
@isTest
private class gdaControllerExtensionTest {
	@testSetup		//Method to create test data for this class
	static void createTestData() {
		List<GFrm__c> lstGF = new List<GFrm__c>();
		
		GFrm__c gf = new GFrm__c();
		gf.Name = 'Test';
		gf.Select_One_Art_Ed__c = 'Test Value Art';
		gf.SelectOne_MFA__c = 'Test Value MFA';
		gf.SelectProgram__c = 'MA';
		lstGF.add(gf);
		
		gf = new GFrm__c();
		gf.Name = 'Test';
		gf.Select_One_Art_Ed__c = 'Test Value Art';
		gf.SelectOne_MFA__c = 'Test Value MFA';
		gf.SelectProgram__c = 'AED';
		lstGF.add(gf);
		
		gf = new GFrm__c();
		gf.Name = 'Test';
		gf.Select_One_Art_Ed__c = 'Test Value Art';
		gf.SelectOne_MFA__c = 'Test Value MFA';
		gf.SelectProgram__c = 'enroll';
		lstGF.add(gf);
		
		insert lstGF();
	} // END static void createTestData() {
	
	@isTest
	static void testMethod_MA() {
		GFrm__c gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c 
						WHERE SelectProgram__c = 'MA' LIMIT 1][0];
		Test.startTest();
			ApexPages.StandardController stdCont = new ApexPages.StandardController(gf);
			gdaControllerExtension gdaCE = new gdaControllerExtension(stdCont);
			gdaCE.mfarten();
		Test.stopTest();
		System.assert(gf.Select_One_Art_Ed__c, null);
	} // END static void testMethod_MA() {
	
	@isTest
	static void testMethod_AED() {
		GFrm__c gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c 
						WHERE SelectProgram__c = 'AED' LIMIT 1][0];
		Test.startTest();
			ApexPages.StandardController stdCont = new ApexPages.StandardController(gf);
			gdaControllerExtension gdaCE = new gdaControllerExtension(stdCont);
			gdaCE.mfarten();
		Test.stopTest();
		System.assert(gf.SelectOne_MFA__c, null);
	} // END static void testMethod_AED() {
	
	@isTest
	static void testMethod_Enroll() {
		GFrm__c gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c 
						WHERE SelectProgram__c = 'enroll' LIMIT 1][0];
		Test.startTest();
			ApexPages.StandardController stdCont = new ApexPages.StandardController(gf);
			gdaControllerExtension gdaCE = new gdaControllerExtension(stdCont);
			gdaCE.mfarten();
		Test.stopTest();
		System.assert(gf.SelectOne_MFA__c, null);
	} // END static void testMethod_Enroll() {
} // END private class gdaControllerExtensionTest {

Please replace the values as per your suitability as I have considered values as per my understanding. Their data type may be different.
It should work for you if does not then please let me know.
mac adminmac admin
Hi Rohit,
Thanks for the reply, I was getting the below error.
Error: Compile Error: Method does not exist or incorrect signature: lstGF() at line 28 column 16

Can you please help me over here.
Rohit B ☁Rohit B ☁
Please replace this with 
insert lstGF;
By mistake I put parenthesis with list name. 

 
mac adminmac admin
Hi Rohit,
Getting error as below.
Error: Compile Error: Method does not exist or incorrect signature: System.assert(String, NULL) at line 40 column 9

Can you help me over here.
Rohit B ☁Rohit B ☁
You can comment this assert line or just before assert line put this query
GFrm__c gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c WHERE SelectProgram__c = 'enroll' LIMIT 1][0];
with the respective condition in WHERE.
Rohit B ☁Rohit B ☁
Here is the updated test class
@isTest
private class gdaControllerExtensionTest {
	@testSetup		//Method to create test data for this class
	static void createTestData() {
		List<GFrm__c> lstGF = new List<GFrm__c>();
		
		GFrm__c gf = new GFrm__c();
		gf.Name = 'Test';
		gf.Select_One_Art_Ed__c = 'Test Value Art';
		gf.SelectOne_MFA__c = 'Test Value MFA';
		gf.SelectProgram__c = 'MA';
		lstGF.add(gf);
		
		gf = new GFrm__c();
		gf.Name = 'Test';
		gf.Select_One_Art_Ed__c = 'Test Value Art';
		gf.SelectOne_MFA__c = 'Test Value MFA';
		gf.SelectProgram__c = 'AED';
		lstGF.add(gf);
		
		gf = new GFrm__c();
		gf.Name = 'Test';
		gf.Select_One_Art_Ed__c = 'Test Value Art';
		gf.SelectOne_MFA__c = 'Test Value MFA';
		gf.SelectProgram__c = 'enroll';
		lstGF.add(gf);
		
		insert lstGF;
	} // END static void createTestData() {
	
	@isTest
	static void testMethod_MA() {
		GFrm__c gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c 
						WHERE SelectProgram__c = 'MA' LIMIT 1][0];
		Test.startTest();
			ApexPages.StandardController stdCont = new ApexPages.StandardController(gf);
			gdaControllerExtension gdaCE = new gdaControllerExtension(stdCont);
			gdaCE.mfarten();
		Test.stopTest();
		gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c 
						WHERE SelectProgram__c = 'MA' LIMIT 1][0];
		System.assertEquals(gf.Select_One_Art_Ed__c, null);
	} // END static void testMethod_MA() {
	
	@isTest
	static void testMethod_AED() {
		GFrm__c gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c 
						WHERE SelectProgram__c = 'AED' LIMIT 1][0];
		Test.startTest();
			ApexPages.StandardController stdCont = new ApexPages.StandardController(gf);
			gdaControllerExtension gdaCE = new gdaControllerExtension(stdCont);
			gdaCE.mfarten();
		Test.stopTest();
		gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c 
						WHERE SelectProgram__c = 'AED' LIMIT 1][0];
		System.assertEquals(gf.SelectOne_MFA__c, null);
	} // END static void testMethod_AED() {
	
	@isTest
	static void testMethod_Enroll() {
		GFrm__c gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c 
						WHERE SelectProgram__c = 'enroll' LIMIT 1][0];
		Test.startTest();
			ApexPages.StandardController stdCont = new ApexPages.StandardController(gf);
			gdaControllerExtension gdaCE = new gdaControllerExtension(stdCont);
			gdaCE.mfarten();
		Test.stopTest();
		gf = [SELECT Id, Name, Select_One_Art_Ed__c, SelectOne_MFA__c, SelectProgram__c FROM GFrm__c 
						WHERE SelectProgram__c = 'enroll' LIMIT 1][0];
		System.assertEquals(gf.SelectOne_MFA__c, null);
	} // END static void testMethod_Enroll() {
} // END private class gdaControllerExtensionTest {

Try this.
This was selected as the best answer
mac adminmac admin
Hi Rohit,
While running test the methods are failed and showing the below error.

Error MessageSystem.DmlException: Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, SelectOne(MFA): bad value for restricted picklist field: MFA Illustration: [SelectOne_MFA__c]
Stack TraceClass.gdaControllerExtensionTest.createTestData: line 28, column 1

Can you please help me why I'm facing this error.

Thanks in advance.
Rohit B ☁Rohit B ☁
Hi Mac,
I've given test values and asked you to replace with the valid values because I was not aware of the type and values associated with those fields. So this error relates to this. Please replace the value of below fields with the valid values exists in the system:-
Name
Select_One_Art_Ed__c 
SelectOne_MFA__c

It should work then..