0

Posting the code here as SampleReports and its associated test class. Always says that line 16,17,18 and 19 are not covered inspite of the test method that I have written.

public class SampleReports{
    Sample_Report__c sample= new Sample_Report__c();
    public static String recordid{get;set;}
    public SampleReports(ApexPages.StandardController controller) {
    recordId = ApexPages.CurrentPage().getParameters().get('ID');
    }
    public PageReference onLoad() {
        sample = [Select Id,Sample_status__c from Sample_Report__c where ID=:recordid];
        if(sample.Sample_status__c=='Draft'){
        sample.Sample_status__c='Submitted';
        update sample;
        PageReference pg = new PageReference('/'+recordid);
        return pg;
        }
        else{
        ApexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Warning,'Record is already Submitted' );   
        ApexPages.addmessage(msg); 
        PageReference pg = new PageReference('/'+recordid); 
        return null;   
        }
    }
}

@isTest(OnInstall=false)
public with sharing class TestDair {
     @IsTest(OnInstall=false)
    static void myUnitTest() {
        User stdUser  = TestHelper.getStandardUser(true);
         Project__c newProj = new Project__c();
        System.runAs(stdUser){
           Test.startTest();
            List<Sample_Report__c> sampleReports  = [Select Id,Sample_status__c from Sample_Report__c LIMIT 1];
            if(sampleReports.size() > 0){            
            sampleReports[0].Sample_status__c ='Draft';        
            PageReference page = new PageReference('/apex/SampleReports');
            page.getParameters().put('id',sampleReports[0].id);
            Test.setCurrentPageReference(page);  
            ApexPages.StandardController stdCtrl = new ApexPages.StandardController(sampleReports[0]);
            SampleReports controller = new SampleReports(stdCtrl);
            PageReference pf = controller.onLoad();      
            PageReference pftest = new PageReference('/'+sampleReports[0].id);      
            Test.stopTest();
            System.assertEquals(pf , pftest);
         }
        }    
      }
      @IsTest(SeeAllData=true)
     static void testNonDIARReportUpdate(){   
           boolean b = false; 
            User stdUser  = TestHelper.getStandardUser(true);           
            System.runAs(stdUser){             
            List<Sample_Report__c> sampleReports  = [Select Id,Sample_status__c from Sample_Report__c LIMIT 1];
            if(sampleReports.size() > 0){            
            sampleReports[0].Sample_status__c ='Submitted';        
            PageReference page = new PageReference('/apex/SampleReports');
            page.getParameters().put('id',sampleReports[0].id);
            Test.setCurrentPageReference(page);  
            ApexPages.StandardController stdCtrl = new ApexPages.StandardController(sampleReports[0]);
            SampleReports controller = new SampleReports(stdCtrl);
            Test.startTest();
            PageReference pf =  controller.onLoad();
            Test.stopTest();
            List<ApexPages.Message> msgList = ApexPages.getMessages();                                 
            System.assertEquals(pf , null);           
            for(ApexPages.Message msg :  msgList) {
                System.assertEquals( msg.getDetail(),'Record is already Submitted');
                System.assertEquals( msg.getSeverity(),ApexPages.Severity.Warning);
              if (msg.getDetail().contains('Record is already Submitted')) b = true;
            }   
            System.assert(ApexPages.hasMessages()); 
            System.assert(b);            
        }
     }
} 
}
}
5

Besides relying on live data (why?), and the fact that you're apparently violating the "don't perform DML without user interaction" rule (allows XSS-style attacks), and other generally un-optimized code structures, it'd be hard to tell why those lines aren't actually covered. They look like they should be, assuming live data exists.

Let's see how you could write your code more efficiently (barring, of course, the security violations):

public class SampleReports {
    ApexPages.StandardController c;
    public SampleReports(ApexPages.StandardController controller) {
        c = controller;
    }
    public PageReference onLoad() {
        PageReference pr;
        if(c.getId()==null) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'This page only works with existing records.'));
        } else {
            Sample_Report__c record = (Sample_Report__c)c.getRecord();
            if(record.Sample_Status__c=='Draft') {
                record.Sample_Status__c='Submitted';
                pr = c.save();
            } else {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.WARNING, 'Record is already Submitted'));
            }
        }
        return pr;
    }
}

Your tests should start off like this:

Sample_Report__c r = new Sample_Report__c(Name='Stuff', Sample_Status__c='Some Status');
insert r;
// Keep going, use "r" in your controller

I realize you probably have validation rules and other things in place, but here's not the place to test them. I suggest adding a function to your TestHelper class to construct a valid Sample_Report__c record that you can use for testing purposes.

You could provide a custom setting for validation rules and Apex Code triggers to bypass their validation when running under certain scenarios, such as unit tests or on demand by a restricted Visualforce page (say, for data loading purposes) to avoid the hassles of tests failing randomly.

0

In your test methods you are setting Sample_status__c but you are never updating the record. When your onLoad() method queries the sample record by the id passed in the page parameters, it gets the current saved value in the database. My guess is that in your test methods the sample returned by your query is by chance already in the 'Draft' status, so the first test passes but the second fails (assuming it is returning the same sample, which isn't guaranteed). If you perform an update after setting the sample status in each test, you should get the results you expect. However, it would be far better practice to insert a test record rather than query an existing Sample_Report__c.

Your Answer

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