+ Start a Discussion

Test Class for basic Controller Extension

Trying to write a test class for this controller extension which allows me to update an opportunity and case from a visualforce page:

public with sharing class controlOppty {
    public Case c;
    public Opportunity oppty;
    private ApexPages.StandardController stdController;

    public controlOppty(ApexPages.StandardController controller) {
        this.c= (Case)controller.getRecord();
        this.oppty= c.Opportunity_Name__r ;
        this.stdController= controller;

    public PageReference saveRecord() {
        PageReference ret = stdController.save();
        system.assertNotEquals(null, c.Id);        
        update oppty;
        update c;
        return ret;

I've written this:

private class TestControlOppty{

    static testMethod void TestControlOppty() {
        Opportunity testOpportunity = new Opportunity();
        testOpportunity.Name = 'Test Opportunity For Test user';
        testOpportunity.StageName = 'Quoted';
        testOpportunity.LeadSource = 'Referral';
        testOpportunity.OwnerId = UserInfo.getUserId();
        testOpportunity.CloseDate = System.today();
        testOpportunity.RecordTypeID = '01280000000BJpn';
        testOpportunity.Amount = Integer.valueOf(Math.random() * 1000);
        testOpportunity.Policy_Type__c = 'Home';
        testOpportunity.Policy_Type_Level_II__c = 'Home';
        testOpportunity.Proof_of_Alarm__c = true;
        testOpportunity.Proof_of_Prior__c = false;
        testOpportunity.Proof_of_Flood_Coverage__c = false;
        testOpportunity.Proof_of_Garaging_Address__c = true;
        testOpportunity.Proof_of_Home_Ownership__c = false;
        testOpportunity.Proof_of_Garaging_Address__c = false;
        testOpportunity.Proof_of_Health_Insurance__c = false;
        testOpportunity.Proof_of_New_Purchase__c = false;
        testOpportunity.Proof_of_Residency__c = false;
        testOpportunity.Proof_of_Wind_Coverage__c = false;
        testOpportunity.Good_Student_Discount__c = true;        
        testOpportunity.Defensive_Driving_Cert__c = true;
        testOpportunity.Appraisal_For_Scheduled_Property__c = true;
        testOpportunity.Roof_Certificate__c = true;
        testOpportunity.Bill_of_Sale__c = false;
        testOpportunity.X4_Point_Inspection__c = true;
        testOpportunity.Wind_Mitigation_Certificate__c = true;
        testOpportunity.Photos__c = false;
        testOpportunity.Other_Trailing_Doc__c = 'Document';
        insert testOpportunity;


        Case c1 = new Case(Opportunity_Name__c = testOpportunity.Id, Subject = 'Test Case', Status = 'New', Due_Date__c = system.today(), Outcome__c = 'Document Received');
                insert c1;
        ApexPages.StandardController sc = new ApexPages.StandardController(c1);
        ControlOppty conOpp = new ControlOppty(sc);
        update c1;
        update testOpportunity;

        List<Case> c = [SELECT Id FROM Case];

And have coverage for:

User-added image

Any help getting these lines covered is appreciated.

Azhar Aziz GAzhar Aziz G
Hey add this script of code after "ControlOppty conOpp = new ControlOppty(sc);" code.
Now my test fails with the following message:

Pass/Fail Fail
Error Message System.NullPointerException: Attempt to de-reference a null object
Stack Trace Class.controlOppty.saveRecord: line 16, column 1
Class.TestControlOppty.TestControlOppty: line 44, column 1