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
Rishabh GoelRishabh Goel 

I need a test class for the following extension class. Can anyone help me with this...

public class my_extension {   
    public TCOM__c t;
    ApexPages.StandardController sController;
    
    public my_extension(ApexPages.StandardController controller) {
        sController = controller;
        t= (TCOM__c )controller.getRecord();
    } 
    
    public ApexPages.PageReference saveNew(){
        sController = new ApexPages.StandardController(t);
        PageReference p=null;
        try{
            sController.save();
            p=new PageReference('/apex/vf40');
            p.setRedirect(true);
        }
        catch(Exception e) {
            return null;
        }
        return p;
        
    }
}
Best Answer chosen by Rishabh Goel
Waqar Hussain SFWaqar Hussain SF
@isTest
public class my_extension_Test{
	
	public static testmethod void UnitTest(){
		
		TCOM__c TestRecord = new TCOM__c();
		TestRecord.Name = 'Test';
		//enter all required fields
		insert TestRecord;
		
		ApexPages.StandardController sc = new ApexPages.StandardController(TestRecord);
		my_extension me = new my_extension();
		me.saveNew();
		
	}
}

 

All Answers

Waqar Hussain SFWaqar Hussain SF
@isTest
public class my_extension_Test{
	
	public static testmethod void UnitTest(){
		
		TCOM__c TestRecord = new TCOM__c();
		TestRecord.Name = 'Test';
		//enter all required fields
		insert TestRecord;
		
		ApexPages.StandardController sc = new ApexPages.StandardController(TestRecord);
		my_extension me = new my_extension();
		me.saveNew();
		
	}
}

 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
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 Apex to access
- Verify the behaviour with asserts.

Try to update the code like below
@isTest
public class my_extension_Test{
	
	public static testmethod void UnitTest(){
		
		TCOM__c TestRecord = new TCOM__c();
		TestRecord.Name = 'Test';
		//enter all required fields
		insert TestRecord;
		
		ApexPages.StandardController sc = new ApexPages.StandardController(TestRecord);
		my_extension me = new my_extension(sc);
		me.saveNew();
		
	}
}

NOTE:- You need to pass StandardController object in my_extension.
 
Rishabh GoelRishabh Goel
I already did that earlier but it still isin't working.
 
Amit Chaudhary 8Amit Chaudhary 8
What error you are getting ?
Rishabh GoelRishabh Goel
Thankyou Guys it worked. It was an Internal Error..
Thanks Alot !