+ Start a Discussion
Nagarjun TNagarjun T 

create test class for account detail page class

Hello, Here iam representing my code for this class i have to create @Test class

public class TRN_AccountDetailPage_Ctrl {
    //Getter setter //Property
    public Account acct{Get;Set;}
    //Constructor same sa class name
    public TRN_AccountDetailPage_Ctrl(){
    
        string strid = apexpages.currentpage().getparameters().get('id');
         if(strid !=''&& strid !=null){
          acct =[select id,name,phone,Accountnumber,Billingcity,billingcountry,billingstreet,fax from Account where id=:strid];
         }
     }
}
Best Answer chosen by Nagarjun T
Amit Chaudhary 8Amit Chaudhary 8
Hi Nagarjun,

I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm


You write a test class for this the same way that you would any other:
- Set up some data for the controller to access (Account, Contact etc)
- Execute a method/methods:-
- Verify the behaviour with asserts.

You Sample test class should be like below
@isTest 
public class TRN_AccountDetailPage_CtrlTest 
{
	static testMethod void testMethod1() 
	{
		
		Account testAccount = new Account();
			testAccount.Name='Test Account' ;
		insert testAccount;

		
		Test.StartTest(); 

			apexpages.currentpage().getparameters().put('id',testAccount.id);
			TRN_AccountDetailPage_Ctrl   obj = new TRN_AccountDetailPage_Ctrl();

		Test.StopTest();
	}
}
Let us know if this will help you

 

All Answers

Niraj Kr SinghNiraj Kr Singh
Hi Nagarjun,

Check this code and follow this link as well.
If it is working fine, plz mark it as your ans for other person refernces.

https://developer.salesforce.com/forums/?id=9060G000000Bj8MQAS (http://If it is working fine, plz mark it as your ans for other person refernces. https://developer.salesforce.com/forums/?id=9060G000000Bj8MQAS)
@isTest
public class TRN_AccountDetailPage_Ctrl_Test {
static testMethod void test()
{
   Account objTestAcc = new Account();
   objTestAcc.Name='Test Acc';
   //Add other fields here if needed. 
  
   insert objTestAcc;

   test.startTest(); 
   PageReference pageRef = Page.AccountPlan; // Add your VF page Name here...
   pageRef.getParameters().put('id', String.valueOf(objTestAcc.Id));
   Test.setCurrentPage(pageRef);
   TRN_AccountDetailPage_Ctrl objTRNAcc = new TRN_AccountDetailPage_Ctrl();
   System.assertEquals(objAcc.id, objTRNAcc.acct.id);
   test.stoptest();
}

 
Amit Chaudhary 8Amit Chaudhary 8
Hi Nagarjun,

I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm


You write a test class for this the same way that you would any other:
- Set up some data for the controller to access (Account, Contact etc)
- Execute a method/methods:-
- Verify the behaviour with asserts.

You Sample test class should be like below
@isTest 
public class TRN_AccountDetailPage_CtrlTest 
{
	static testMethod void testMethod1() 
	{
		
		Account testAccount = new Account();
			testAccount.Name='Test Account' ;
		insert testAccount;

		
		Test.StartTest(); 

			apexpages.currentpage().getparameters().put('id',testAccount.id);
			TRN_AccountDetailPage_Ctrl   obj = new TRN_AccountDetailPage_Ctrl();

		Test.StopTest();
	}
}
Let us know if this will help you

 
This was selected as the best answer