I have problems creating an appropriate testclase to my controller extension. I have a VF page on opportunities which creates for a certain type of opportunity 4 independent (quarterly) opportunity records through the extension when the user clicks the save button.

Here is the apex code for the extensions:

public  class ExtEUQuaterlyOpp {

// the standard controller

private ApexPages.StandardController stdCtrl {get; set;}
Opportunity newOpp{get;set;}
Opportunity newOpp2{get;set;}
Opportunity newOpp3{get;set;}
Opportunity newOpp4{get;set;}

public ExtEUQuaterlyOpp(ApexPages.StandardController std)

    newOpp = (Opportunity)std.getRecord();
    newOpp.Name   = ApexPages.currentPage().getParameters().get('opp3');
    newOpp.AccountId = ApexPages.currentPage().getParameters().get('opp4_lkid');
    newOpp.StageName = ApexPages.currentPage().getParameters().get('opp11');

public PageReference save()
      newOpp2 = new Opportunity(Name = 'Opp2',
                                AccountId = newOpp.AccountId,
                                Fund__c = newOpp.Fund__c,
                                StageName = newOpp.StageName,

      newOpp3 = new Opportunity(Name = 'Opp3',
                                AccountId = newOpp.AccountId,
                                Fund__c = newOpp.Fund__c,
                                StageName = newOpp.StageName,

      newOpp4 = new Opportunity(Name = 'Opp4',
                                AccountId = newOpp.AccountId,
                                Fund__c = newOpp.Fund__c,
                                StageName = newOpp.StageName,
    try {
        insert newOpp;  
        insert newOpp2;    
        insert newOpp3;
        insert newOpp4;            
        pagereference pf= new pagereference('/'+newOpp.AccountId);
        return pf; 
        catch(Exception ex){
    return null;

And here is my first try of the testclass:

public class ExtEUQuaterlyOpp_Test {

private static testMethod void testExtEUQuaterlyOpp() {

    Account a = new Account();
    a.Name = 'Test';
    insert a;

    ApexPages.StandardController stdOpp = new ApexPages.StandardController(a);
    ExtEUQuaterlyOpp objExtEUQuaterlyOpp  = new ExtEUQuaterlyOpp(stdOpp);

    PageReference pageRef = Page.quaterlyopp; // Add your VF page Name here
    pageRef.getParameters().put('opp3', 'TestOpp');
    pageRef.getParameters().put('opp4_lkid', a.Id);


I have a code coverage of 2%. Has anyone an idea how to start with ?

  • 1
    When your question is "where should I start" the answer is always Trailhead. Such questions tend to be harshly received here, and that platform was specifically designed for beginners.
    – Adrian Larson
    Mar 15 '17 at 16:14

If you have no clue where to start, Trailhead is the place for you. Give yourself a basis to get the ball rolling on your own and ask a more specific question.

That said, if you are focused on coverage, you have entirely missed the point of Unit Testing. From How to Write Good Unit Tests:

Verify the results are correct

Verifying that your code works as you expect it to work is the most important part of unit testing. It’s also one of the things that Force.com developers commonly neglect. Unit tests that do not verify the results of the code aren’t true unit tests. They are commonly referred to as smoke tests, which aren’t nearly as effective or informative as true unit tests.

A good way to tell if unit tests are properly verifying results is to look for liberal use of the System.assert() methods. If there aren’t any System.assert() method calls, then the tests aren’t verifying results properly. And, no, System.assert(true); doesn’t count.

It is fundamentally important that, as you learn about how to write unit tests properly, you understand that assertions are the cornerstone of what you're trying to achieve. Without them, your tests do not really protect the functionality whose implementation they are supposed to guarantee.

In addition to completing the above Trailhead module, I recommend you read all of the following:

Once you've done all that, you should be able to write most of the test class yourself, and be equipped to come back here with more specific questions about where you get stuck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.