Skip to main content
added 1564 characters in body
Source Link
Adrian Larson
  • 151.4k
  • 38
  • 247
  • 431

You obviously have a few different execution paths:

  • Your Id value is null
  • Your Id value is populated and the update call succeeds
  • Your Id value is populated and the update call fails

Note how my pseudocode test above calls the class instance extension, not controller. Please take a moment to read What is the Difference Between Controllers and Extensions? and understand what sort of class definition you are writing. There are a couple other things you could improve about your class, specifically you should use the controller.getId method rather than the case-sensitive getParameters() approach.

public with sharing class MyExtension
{
    final Id recordId;
    final Contract record;
    public MyExtension(ApexPages.StandardController controller)
    {
        // you are accepting a controller as a parameter
        // this class is actually an EXTENSION

        recordId = controller.getId();
        record = (Contract)controller.getRecord();
    }
    public PageReference myMethod()
    {
        if (recordId == null)
        {
            // you should really add an error message to the page here
            return null;
        }
        
        // query related record and set some fields
        try
        {
            update relatedRecord;
        }
        catch (DmlException dmx)
        {
            // you will greatly improve the user experience
            // if you add some basic error handling
            ApexPages.addMessages(dmx);
            return null;
        }
        
        return somePageReference;
    }
}

One final note, it is much better practice to construct the view using the StandardController. The end of your method should simply read:

return new ApexPages.StandardController(relatedRecord).view();

You also don't need to call setRedirect(true) since that is the default value.

You obviously have a few different execution paths:

  • Your Id value is null
  • Your Id value is populated and the update call succeeds
  • Your Id value is populated and the update call fails

Note how my pseudocode test above calls the class instance extension, not controller. Please take a moment to read What is the Difference Between Controllers and Extensions? and understand what sort of class definition you are writing. There are a couple other things you could improve about your class, specifically you should use the controller.getId method rather than the case-sensitive getParameters() approach.

public with sharing class MyExtension
{
    final Id recordId;
    final Contract record;
    public MyExtension(ApexPages.StandardController controller)
    {
        // you are accepting a controller as a parameter
        // this class is actually an EXTENSION

        recordId = controller.getId();
        record = (Contract)controller.getRecord();
    }
    public PageReference myMethod()
    {
        if (recordId == null)
        {
            // you should really add an error message to the page here
            return null;
        }
        
        // query related record and set some fields
        try
        {
            update relatedRecord;
        }
        catch (DmlException dmx)
        {
            // you will greatly improve the user experience
            // if you add some basic error handling
            ApexPages.addMessages(dmx);
            return null;
        }
        
        return somePageReference;
    }
}

One final note, it is much better practice to construct the view using the StandardController. The end of your method should simply read:

return new ApexPages.StandardController(relatedRecord).view();

You also don't need to call setRedirect(true) since that is the default value.

Source Link
Adrian Larson
  • 151.4k
  • 38
  • 247
  • 431

Before you start working on coverage, you should spend some time understanding the chief goal of unit testing. Coverage is simply a side effect of well written tests, it should never be the primary goal.

As noted in How to Write Good Unit Tests (emphasis mine):

###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.

Your test does not contain a single assertion, so you have not yet written a unit test.


Now, as for how to cover this particular code, you do what you would with any other code in your application, run the methods you want to test. Your testmethod never once calls autorun(). You will need to call your method more than once to hit each execution path. The basic structure of your method should look as follows:

static testmethod void testAutoRun_UseCase1()
{
    // set up data for a specific execution path

    Test.startTest();
        extension.autoRun();
    Test.stopTest();
    
    // verify behavior using assertions
}
static testmethod void testAutoRun_UseCase2()
{
    // set up data for a specific execution path

    Test.startTest();
        extension.autoRun();
    Test.stopTest();
    
    // verify behavior using assertions
}