0

Hi I'm hoping someone can help, I have a custom controller with a simple SQL statement that I can't seem to get a test class working, here is the controller:

public class Example_Controller {
    
public Example__c ExampleFields { get; set; }
    
    public Example_Controller() {
        ExampleFields = [SELECT Id, Site__r.Name, Issue_No__c,Site__r.Account.Name,Quote__r.Name,Compliance_Sheet_Design_Ref__c,Example_Checked_By__c,Example_Prepared_By__c,Work_Area__c FROM RAMS__c 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }
}

Here is my test class which returns zero test coverage:

@isTest(SeeAllData=true)
public class Example_ControllerTest {
    @istest
    public static void testCurrentExample(){

        Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
    
        Product2 prod = new Product2(Name='test1',Family='Liner', IsActive=true);
        insert prod;
    
        PricebookEntry pbe = new PricebookEntry (Product2Id=prod.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
        insert pbe;
        
        Opportunity o = new Opportunity(Name='Testing Opportunity', StageName='New', CloseDate=system.today());
        insert o; 
        
        Quote q = new Quote(Name='Test', Pricebook2Id = standardPb.id, OpportunityId=o.id);
        insert q; 
        
        Example__c ExampleFields = new Example__c(Issue_No__c='0001',Example_Prepared_By__c='Jack',Example_Checked_By__c='Tom',Compliance_Sheet_Design_Ref__c='3121',Site__c=o.id,Work_Area__c='Hospital Building',Quote__c=q.id);
        insert ExampleFields;
        
        //ApexPages.StandardController sc = new ApexPages.standardController(ExampleFields);
        //Apexpages.StandardController sc = new Apexpages.StandardController(ExampleFields);
        //Example_Controller ext = new Example_Controller (sc);
        //Example_Controller  a=new Example_Controller ();
        //ApexPages.StandardController sc = new ApexPages.standardController(ExampleFields);

        //Example_Controller controller = new Example_Controller (sc);

        List <Example__c> Example = [SELECT id,Issue_No__c,Example_Prepared_By__c,Example_Checked_By__c,Compliance_Sheet_Design_Ref__c,Site__c,Work_Area__c,Quote__c FROM Example__c WHERE Id = :ExampleFields.Id];

        System.assertequals('0001', Example[0].Issue_No__c);
        

    }
}
1

1 Answer 1

0

Found the answer, I needed to put the page in to test and then 'create' a new controller:

PageReference pageRef = Page.Example
pageRef.getParameters().put('id', String.valueOf(ExampleFields.Id));
Test.setCurrentPage(pageRef); 
Example_Controller testAccPlan = new Example_Controller();
Test.StopTest();

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.