0

I have a deep clone class that is called from a VF page. The class executes perfectly, I just cannot get my test class to work properly. I'm trying to verify that the cloned record is created within the method but for some reason, the class keeps failing.

I'm getting the "attempt to de-reference null object at line 48 of my test class

// make sure the user is sent to the correct url
System.assertEquals(ref.getUrl(),redir.getUrl());

If I comment that section out, I get an error saying I have no rows at Line 51

// check that the new opp was created successfully
        Opportunity newOpp = [select id from Opportunity where id = :ext.newRecordId];
        System.assertNotEquals(newOpp, null);

Basically, it looks like the ext.newRecordId aspect is not being transferred to the test class.

Any help is appreciated.

Class/Extension

    public class cloneRenewalOpp {

    //added an instance varaible for the standard controller
    @testVisible private ApexPages.StandardController controller {get; set;}
     // add the instance for the variables being passed by id on the url
    @testVisible private Opportunity opp {get;set;}
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS
    public ID newRecordId {get;set;}

    // initialize the controller
    public cloneRenewalOpp(ApexPages.StandardController controller) {

        //initialize the standard controller
        this.controller = controller;
        // load the current record
        opp = (Opportunity)controller.getRecord();

    }

    // method called from the VF's action attribute to clone the opp
    public PageReference cloneWithItems() {

         // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         Opportunity newOpp;
         RecordType rt = [SELECT Id FROM RecordType WHERE Name = 'Renewal Business' AND sObjectType='Opportunity' Limit 1];

         try {

              //copy the purchase order - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             opp = [select Id, Name, Amount, StageName, CloseDate, Contract_Start_Date__c, Contract_End_Date__c, AccountId, Agency__c, Partnership_Type__c, vCPM_Rate_Avg__c, Primary_Vertical__c, Secondary_Vertical__c, Tertiary_Vertical__c, Primary_Age_Group__c, Primary_Demographic__c, Competitive_Separation__c from Opportunity where id = :opp.id];
             newOpp = opp.clone(false);
             newOpp.Name = opp.Renewal_Name__c;
             newOpp.CloseDate = opp.Contract_End_Date__c;
             newOpp.StageName = 'Value Proposition';
             newOpp.Parent_Opportunity__c = opp.Id;
             newOpp.RecordTypeId = rt.id;
             insert newOpp;

             // set the id of the new po created for testing
               newRecordId = newOpp.id;

             // copy over the line items - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             List<Campaign_Line_Item__c> cliList = new List<Campaign_Line_Item__c>();
             for (Campaign_Line_Item__c cli : [Select Id, Flight_Start_Date__C, Flight_End_Date__c, Budget__c From Campaign_Line_Item__c where Opportunity__c = :opp.id]) {
                  Campaign_Line_Item__c newCLI = cli.clone(false);
                  newCLI.Opportunity__c = newOpp.id;
                  cliList.add(newCLI);
             }
             insert cliList;

         } catch (Exception e){
             // roll everything back in case of error
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
         }

        return new PageReference('/'+newOpp.id+'/e?retURL=%2F'+newOpp.id);
    }
}

Test Class

@isTest
private class Test_cloneRenewalOpp {

    static testMethod void test_clonerenewaloppcontroller() {

       // setup a reference to the page the controller is expecting with the parameters
        PageReference pref = Page.cloneRenewalOpp;
        Test.setCurrentPage(pref);

        // setup an account
        Account acct = new Account();
        acct.Name = 'PSAV 6FOO';
        acct.Type = 'Supplier';
        acct.BillingState = 'TX';
        insert acct;

        // create new opp record
        Opportunity opp = new Opportunity();
        opp.Name = 'test opp name';
        opp.StageName = 'Prospecting';
        opp.CloseDate = system.date.today();
        opp.AccountId = acct.id;
        insert opp;

        // create a line item for the opp
        Campaign_Line_Item__c cli = new Campaign_Line_Item__c();
        cli.Opportunity__c = opp.Id;
        cli.flight_Start_Date__c = system.today();
        cli.Flight_End_Date__c = system.today().addMonths(12);
        cli.Budget__c = 1000000;
        insert cli;

        // Construct the standard controller
        ApexPages.StandardController con = new ApexPages.StandardController(opp);

        // create the controller
        cloneRenewalOpp ext = new cloneRenewalOpp(con);

        // Switch to test context
        Test.startTest();

        // call the cloneWithItems method
        PageReference ref = ext.cloneWithItems();
        // create the matching page reference
        PageReference redir = new PageReference('/'+ext.newRecordId+'/e?retURL=%2F'+ext.newRecordId);

        // make sure the user is sent to the correct url
        System.assertEquals(ref.getUrl(),redir.getUrl());

        // check that the new opp was created successfully
        Opportunity newOpp = [select id from Opportunity where id = :ext.newRecordId];
        System.assertNotEquals(newOpp, null);
        // check that the line item was created
        List<Campaign_Line_Item__c> newItems = [Select c.Id From Campaign_Line_Item__c c where Opportunity__c = :newOpp.id];
        System.assertEquals(newItems.size(),1);

        // Switch back to runtime context
        Test.stopTest();

    }

}
  • 4
    It's probably likely that the first half of the assert is failing. "ref" being null. System.assertEquals(ref.getUrl(),redir.getUrl()); << that's line 48 from what I can tell. So go into your clonewithitems method it looks like you are probably going into the catch statement where it returns null. Add another system.assert before line 48: System.assertNotEquals(null, ref); – Mike Katulka May 5 '15 at 15:03
  • 4
    The attempt to de-reference a null pointer on that line could be due to the variable ref being set to null, which would happen if an exception occurred in the cloneWithItems function. To test this idea, try adding System.assertNotEquals(null, ref); – Doug B May 5 '15 at 15:03
  • 1
    @MikeKatulka well said! – Doug B May 5 '15 at 15:04
  • 1
    @Doug B, you too! – Mike Katulka May 5 '15 at 15:04

Your Answer

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

Browse other questions tagged or ask your own question.