I have only 73% code coverage, however, I've created multiple tests, all which pass.

Here is the controller code:

public class CaseController {
    public ApexPages.StandardController stdCntrlr {get; set;}
    public CaseController(ApexPages.StandardController controller) {
        stdCntrlr = controller;
    public Case cs{get; set;}
//    public CaseController(){
//        cs = new Case();
    public PageReference save(){
       if(cs != null) {
//            upsert cs;
        } else {
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.CONFIRM,'Successfully inserted Case.'));
            PageReference pr = new PageReference('/apex/CasePage');
            return pr;
//        return null;

This is my test class

public class CaseTest {
    static testMethod void testMethod1(){
        Case testCase = new Case();
        testCase.Status = 'New';
        testCase.Origin = 'Phone';
        testCase.Subject = 'test';
        testCase.Description = 'test';
        insert testCase;
        ApexPages.StandardController sc = new ApexPages.StandardController(testCase);
        CaseController testCasePage = new CaseController(sc);
        PageReference pageRef = Page.CasePage;
        pageRef.getParameters().put('id', String.valueOf(testCase.Id));
    static testMethod void bulkTestMethod2(){
        integer caseCount = 0;
        list<Case> cs = new List<Case>();
        while (caseCount<=151){
            caseCount +=1; 
        insert cs;
    static testMethod void positiveTestMethod3(){
        Case cs = new Case(Status = 'New');
    static testMethod void negativeTestMethod4(){
        Case cs = new Case();
        cs.Status = Null;
        insert cs;
        Database.SaveResult sr = Database.insert(cs, false);
        system.assertEquals(false, sr.isSuccess());

The lines with "//" is what is not covered in the test. I dont know why it doesnt cover those lines when Ive created a test class in which creates and inserts a new Case.

  • 2
    Try going through the suggestions in the answer to this canonical question: salesforce.stackexchange.com/q/244794
    – Moonpie
    Sep 16, 2021 at 17:05
  • The last part of your post says your commented code doesn't get covered - which is true and by design. You can't cover commented code because it doesn't have any functional use. That being said, you should still get full coverage even with lines that are commented out.
    – nbrown
    Sep 16, 2021 at 17:37
  • 2
    @nbrown : I think the OP means that none of the code is actually commented out in their system, they just marked the lines which are not getting covered with "//" in their question so that we would know. Which is obviously not the clearest way of showing us.
    – Moonpie
    Sep 16, 2021 at 17:52

1 Answer 1


The golden rule of unit testing is you only gain coverage for code that is executed as part of running a test method.

You do have multiple test methods, but only one of them (testMethod1()) actually executes any code in your controller. The rest of your test methods simply insert new records using regular DML (and thus they do not contribute any code coverage for your controller/extension).

You're also missing any meaningful assertions (and making appropriate assertions is the most important part of unit testing).

How do I increase my code coverage, or why can't I cover these lines? is definitely something you should check out, as it contains general advice and resources on code coverage.

As for a little more specific advice:

  • Every one of your test methods for this controller should have new CaseController(sc) (or something similar) in it
  • You should be using the @isTest annotation instead of the deprecated testmethod keyword
  • When you make assertions, the things you want to assert are the results of running your controller method. That could be things like making sure a record was inserted, that you get a null return value from calling your save() method, or that the stdCntrlr property has been set after calling one of your constructors
  • 2
    +1(00) for "making appropriate assertions is the most important part of unit testing"
    – Moonpie
    Sep 16, 2021 at 17:54

Not the answer you're looking for? Browse other questions tagged .