function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
FrodoFrodo 

Help needed to get 75% test coverage.

I have a fairly simple Apex controller which I cannot figure out how to test.  I can't get above 57% code coverage.  I am not understanding something. Can someone please shed light. I do not have coverage for lines 15,16,17,18,19,20,21,23 Here is the code:

 

public class copyAddress {

 

P4_AssetDeploymentRelationship__c currentDeploy;

 

public copyAddress(ApexPages.StandardController controller) {
        this.currentDeploy = (P4_AssetDeploymentRelationship__c)controller.getRecord();
}

 

public PageReference address()
    {
        if (System.currentPageReference().getParameters().get('id')== null) {
        PageReference redraw = Apexpages.currentPage();
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Parameter Does Not Exist'));
      return redraw; }
      string contactID = [select contact__c from P4_AssetDeploymentRelationship__c where id = :currentDeploy.Id].contact__c;     
      this.currentDeploy.Shipment_St_Address_1__c = [select mailingstreet from contact where id = :ContactID].mailingstreet;
      currentDeploy.Shipment_City__c = [select mailingcity from contact where id = :ContactID].mailingcity;
      currentDeploy.Shipment_State_Province__c = [select mailingstate from contact where id = :ContactID].mailingstate;
      currentDeploy.Shipment_Country__c = [select mailingcountry from contact where id = :ContactID].mailingcountry;
      currentDeploy.Shipment_Postal_Code__c  = [select mailingpostalcode from contact where id = :ContactID].mailingpostalcode;
      upsert currentDeploy;
      return null;
    }
 
    public PageReference persistAddress()
    {
       upsert currentDeploy;
       return null;
    }
    public static testMethod void testCopyAddress() {
    P4_AssetDeploymentRelationShip__c testDeploy = new P4_AssetDeploymentRelationShip__c();
    insert testDeploy;
    ApexPages.StandardController sc = new ApexPages.standardController(testDeploy);
    copyAddress testAddress = new copyAddress(sc);
    PageReference testPage = testAddress.Address();
    System.Assert(testPage != null);
    testPage = testAddress.persistAddress();
    System.AssertEquals(null,testPage);
    P4_AssetDeploymentRelationShip__c x;
    Contact c = new contact(FirstName='James',LastName='Malone',MailingStreet='8102 Dewberry Circle');
     insert c;
    testAddress.currentDeploy = new P4_AssetDeploymentRelationShip__c(contact__c=c.Id);
  
    insert testAddress.currentDeploy;   
       string contactID = [select contact__c from P4_AssetDeploymentRelationship__c where id = :testAddress.currentDeploy.Id].contact__c;     
       System.AssertEquals(testAddress.currentDeploy.contact__c, contactID);
     testAddress.currentDeploy.Shipment_St_Address_1__c = [select mailingstreet from contact where id = :ContactID].mailingstreet;
     upsert testAddress.currentDeploy;
     System.AssertEquals(c.MailingStreet,testAddress.currentDeploy.Shipment_St_Address_1__c);
     testAddress.currentDeploy.Shipment_State_Province__c = [select mailingstate from contact where id = :ContactID].mailingstate;
     upsert testAddress.currentDeploy;
      System.AssertEquals(c.MailingState,testAddress.currentDeploy.Shipment_State_Province__c);
}
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
werewolfwerewolf

if (System.currentPageReference().getParameters().get('id')== null)

 

In your address method you have this call which exits out.  When you call it with your test method, there are no page parameters so there is no ID, so it's not covering the real code.  Pass in an ID instead of getting it from the page param in here.

All Answers

werewolfwerewolf

if (System.currentPageReference().getParameters().get('id')== null)

 

In your address method you have this call which exits out.  When you call it with your test method, there are no page parameters so there is no ID, so it's not covering the real code.  Pass in an ID instead of getting it from the page param in here.

This was selected as the best answer
FrodoFrodo
Thanks Werewolf!  You cut straight to the issue.  I revised things a bit as you suggested and now I have 100% test coverage.