1

I am trying to test my controller extension, Although the final score coverage hit into 100% but when i checked my tests log in my dev console i got Error.

This is the ERROR Message:

System.DmlException: Insert failed. First exception on row 0 with id 0010o00002DZ5DiAAL; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Here is my Test Class Code:

@isTest
public class AcountControllerExtensionTest {

    @isTest static void verifyAccSave(){
        //Create dummy account for controller
        Account acc = new Account();

        //call the current page for testing
        Test.setCurrentPage(Page.NewAccountAndContact);

        //create a standard controller and pass the dummy account controller
        ApexPages.StandardController accountController = new ApexPages.StandardController(acc);

        //Get the Standard Controller ang pass it to controller extension
        AccountControllerExtension ace = new AccountControllerExtension(accountController);

        //Start the Test
        Test.startTest();
        ace.acct.Name = 'Testing';
        ace.cont.AccountId = ace.acct.Id;
        ace.cont.LastName = 'Last Name';
        ace.cont.Email = '[email protected]';
        ace.save();
        PageReference redPage = ace.save();
        Test.stopTest();
        //End Test

        //Verify using assertion
        Account verifyAccountInsert = [SELECT Id FROM Account where Id =: ace.acct.Id];
        System.assert(verifyAccountInsert.Id != NULL);

        //Verify if it's redirect to the account detailed page
        System.assertEquals('/'+verifyAccountInsert.Id, redPage.getUrl());
    }
}

And here is my Controller Extension:

    public class AccountControllerExtension {
    public final Account acct {get;set;}
    public Contact cont {get;set;}

    public AccountControllerExtension(ApexPages.StandardController std){
        this.acct = (Account)std.getRecord();
        this.cont = new Contact();
    }

    public PageReference save(){
        //Insert the account
        insert acct;

        //Pass the Billing address to Contact Mailing Address if the Contact is not empty
        if(cont != NULL){
            cont.MailingStreet = acct.BillingStreet;
            cont.MailingCity = acct.BillingCity;
            cont.MailingState = acct.BillingState;
            cont.MailingPostalCode = acct.BillingPostalCode;
            cont.MailingCountry = acct.BillingCountry;
            cont.AccountId = acct.Id;
            cont.Type__c = 'Subscriber';
            insert cont;
        }
        //Redirect to the Record Detailed Page
        PageReference redirectPage = new PageReference('/' + acct.Id);
        redirectPage.setRedirect(true);
        return redirectPage;
    }
}

I think In my test Class is it needs to add first the account before the contact but in my controller extension I put the insert Account and Contact in the same method. Any help please?

1 Answer 1

3

Your issue is simply that you're calling save() twice.

    ace.save();
    PageReference redPage = ace.save();

After the first call to save(), the acct.Id field is populated by the DML operation - you've persisted that Account. When your test class then calls save() again, you get this error, because you can't insert the already-inserted Account record.

You don't appear to need both calls to save() here. Just remove the first one.

4
  • Thanks a lot again @David, Now it does make sense, My bad.. Feb 8, 2019 at 13:58
  • David still there? Just to ask 1 thing, I have Cancel button in the VF Page, is it really necessary to create a testmethod for the cancel button? And I don't have a cancel method in my Controller Extension. Feb 8, 2019 at 14:08
  • @LeorahSumarong You only need to unit test code that you have written. You don't need to test functionality provided by the standard controller.
    – David Reed
    Feb 8, 2019 at 14:10
  • Very well noted, Thanks @David, I do appreciate your effort. Feb 8, 2019 at 14:23

You must log in to answer this question.

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