0

I have a Code Coverage Failure of 32% I already tested my APEX Class and it's coming out as 27/29 at 93%. I do have the following...

ControllerProposalConvertView = Apex Class
Convert = Custom Link
ConvertView = VF Page
ConvertView2 = VF Page
ControllerProposalConvertViewTest4 = @isTest

What am I missing here? Here are my codes.
ControllerProposalConvertView

public class ControllerProposalConvertView {
public Id pId;
public String convertedAccountId;

public ControllerProposalConvertView(ApexPages.StandardController stdController){
    pId = ApexPages.CurrentPage().getParameters().get('id');
    System.Debug('#######leadId:' + pId);
}


public PageReference convert(){

    try{
    Proposal__c p = [SELECT Id, name, Already_Converted__c, Property__c, Square_Footage__c, Lot_Size__c, Lot__c, Cap_Rate__c, Year_Built__c, Zoning__c, Term__c, Lease_Type__c, NOI__c, Lease_Commencement_Date__c, Rent_Commencement_Date__c, Lease_Expiration_Date__c, Years_Remaining__c, Lease_Notes__c FROM Proposal__c WHERE Id=:pId LIMIT 1];

    if (p.Already_Converted__c  =='Not Converted'){
    Listing__c c=new Listing__c(Name=p.Name, Property__c=p.Property__c, Square_Footage__c=p.Square_Footage__c,  Lot_Size__c=p.Lot_Size__c, Lot__c=p.Lot__c, Cap_Rate__c=p.Cap_Rate__c, Year_Built__c=p.Year_Built__c, Zoning__c=p.Zoning__c, Term__c=p.Term__c, Lease_Type__c=p.Lease_Type__c, NOI__c=p.NOI__c, Lease_Commencement_Date__c=p.Lease_Commencement_Date__c, Rent_Commencement_Date__c=p.Rent_Commencement_Date__c, Lease_Expiration_Date__c=p.Lease_Expiration_Date__c, Years_Remaining__c=p.Years_Remaining__c, Lease_Notes__c=p.Lease_Notes__c);
    System.Debug('#######c :' + c );
    insert c;
    p.Already_Converted__c='Converted';
   update p;
    convertedAccountId = c.Id;
    System.Debug('#######convertedAccountId :' + convertedAccountId );
    }

    else{
            String sServerName = ApexPages.currentPage().getHeaders().get('Host');
    sServerName = 'https://'+sServerName+'/';
    PageReference retPage = new PageReference(sServerName + '/apex/ProposalConvertView2?id='+ pId); 
    retPage.setRedirect(true);
    System.Debug('#######ALREADYCONVERTED' );

    return retPage;
    }

    }

    catch(Exception e){
        System.Debug('#######Error  - Exception [' + e.getMessage() + ']');
        return null;
    }
    String sServerName = ApexPages.currentPage().getHeaders().get('Host');
    sServerName = 'https://'+sServerName+'/';
    System.Debug('#######sServerName :' + sServerName );
    PageReference retPage = new PageReference(sServerName + convertedAccountId); 
    System.Debug('#######retPage :' + retPage );
    retPage.setRedirect(true);


    return retPage;
} 
public PageReference back(){
        String sServerName = ApexPages.currentPage().getHeaders().get('Host');
    sServerName = 'https://'+sServerName+'/';
    System.Debug('#######sServerName :' + sServerName );
    PageReference retPage = new PageReference(sServerName + pId); 
    System.Debug('#######retPage :' + retPage );
    retPage.setRedirect(true);

    return retPage;
}      
}

ControllerProposalConvertViewTest4

   @isTest
   public class ControllerProposalConvertViewTest4 {

   static testMethod void convert(){
    Proposal__c p = new Proposal__c (

    Name = 'Test Tes',
    Already_Converted__c = 'Converted',
    Property__c = 'a007A000001C6CM',
    Zoning__c = 'Zones',
    Lease_Type__c = 'Monthly',
    Lease_Notes__c = 'Ground Lease',
    Lot_Size__c = 999.00,
    Lot__c = 99.00,
    Cap_Rate__c = 999.75,
    Year_Built__c = '1999',
    Term__c = 999,
    NOI__c = 999.12,
    Years_Remaining__c = 99.50,
    Lease_Commencement_Date__c = date.Today(),
    Rent_Commencement_Date__c = date.Today(),
    Lease_Expiration_Date__c = date.Today(),
    Square_Footage__c = 99900);


    insert p;


    PageReference pageRef = Page.ProposalConvertView;
    Test.setCurrentPage(pageRef);
    ApexPages.currentPage().getParameters().put('id',p.Id);
    ApexPages.StandardController std = new ApexPages.StandardController(new Proposal__c());
    ControllerProposalConvertView cont = new ControllerProposalConvertView (std);
    Id pId;


    cont.convert();

    listing__c c = new listing__c (Name = 'Test Tes', 
    Already_Converted__c = 'Converted',
    Property__c = 'a007A000001C6CM',
    Zoning__c = 'Zones',
    Lease_Type__c = 'Monthly',
    Lease_Notes__c = 'Ground Lease',
    Lot_Size__c = 999.00,
    Lot__c = 99.00,
    Cap_Rate__c = 999.75,
    Year_Built__c = '1999',
    Term__c = 999,
    NOI__c = 999.12,
    Years_Remaining__c = 99.50,
    Lease_Commencement_Date__c = date.Today(),
    Rent_Commencement_Date__c = date.Today(),
    Lease_Expiration_Date__c = date.Today(),
    Square_Footage__c = 99900);
    insert c;

    PageReference retPage = Page.ProposalConvertView2;
    Test.setCurrentPage(retPage);
    ApexPages.currentPage().getHeaders().get('sServerName');
    cont.back();

    }

    static testMethod void converttwo(){
    Proposal__c p1 = new Proposal__c (
    Name = 'Test Tes',
    Already_Converted__c = 'Not Converted',
    Property__c = 'a007A000001C6CM',
    Zoning__c = 'Zones',
    Lease_Type__c = 'Monthly',
    Lease_Notes__c = 'Ground Lease',
    Lot_Size__c = 999.00,
    Lot__c = 99.00,
    Cap_Rate__c = 999.75,
    Year_Built__c = '1999',
    Term__c = 999,
    NOI__c = 999.12,
    Years_Remaining__c = 99.50,
    Lease_Commencement_Date__c = date.Today(),
    Rent_Commencement_Date__c = date.Today(),
    Lease_Expiration_Date__c = date.Today(),
    Square_Footage__c = 99900);
    insert p1;

    PageReference pageRef = Page.ProposalConvertView;
    Test.setCurrentPage(pageRef);
    ApexPages.currentPage().getParameters().put('id',p1.Id);
    ApexPages.StandardController std = new ApexPages.StandardController(new Proposal__c());
    ControllerProposalConvertView cont = new ControllerProposalConvertView (std);
    Id pId;

    cont.convert();

    listing__c c = new listing__c (Name = 'Test Tes',
    Already_Converted__c = 'Not Converted',
    Property__c = 'a007A000001C6CM',
    Zoning__c = 'Zones',
    Lease_Type__c = 'Monthly',
    Lease_Notes__c = 'Ground Lease',
    Lot_Size__c = 999.00,
    Lot__c = 99.00,
    Cap_Rate__c = 999.75,
    Year_Built__c = '1999',
    Term__c = 999,
    NOI__c = 999.12,
    Years_Remaining__c = 99.50,
    Lease_Commencement_Date__c = date.Today(),
    Rent_Commencement_Date__c = date.Today(),
    Lease_Expiration_Date__c = date.Today(),
    Square_Footage__c = 99900);
    insert c;

    PageReference retPage = Page.ProposalConvertView2;
    Test.setCurrentPage(retPage);
    ApexPages.currentPage().getHeaders().get('sServerName');
    cont.back();

    }

    }

ConvertView

<apex:page standardController="Proposal__c"     extensions="ControllerProposalConvertView">
<apex:outputPanel layout="block" style="font-weight:bold">
    You are about to convert this proposal to a listing. Are you sure you want to do this?
</apex:outputPanel>
<apex:form >
<apex:commandButton action="{!convert}" value="OK" id="OK" rerender="hiddenBlock"/>
<apex:commandButton action="{!back}" value="Cancel" id="Cancel" /> 
<!--  <apex:commandButton value="Cancel" id="Cancel" onclick="window.alert('You have already created a Listing from this Proposal. Please Press cancel to return to the Proposal page')"/>-->

</apex:form>
<apex:outputPanel layout="block" style="font-weight:bold">   </apex:outputPanel>

</apex:page>

ConvertView2

<apex:page standardController="Proposal__c" extensions="ControllerProposalConvertView">
<apex:outputPanel layout="block" style="font-weight:bold; color:red;">
    This proposal has already been converted to a listing. Please click the button below to return to your proposal.
        </apex:outputPanel>
        <apex:form >
        <apex:commandButton action="{!back}" value="Back" id="Cancel"/>
         </apex:form>

Convert

<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<webLinks>
    <fullName>ConvertCustom</fullName>
    <availability>online</availability>
    <displayType>button</displayType>
    <encodingKey>UTF-8</encodingKey>
    <linkType>url</linkType>
    <masterLabel>Convert</masterLabel>
    <openType>replace</openType>
    <protected>false</protected>
    <url>/apex/ProposalConvertView?id={!Proposal__c.Id}</url>
</webLinks>

I'm just a bit confused. Thanks for looking and your ideas are greatly appreciated.

4
  • Start by checking whether the object with ID a007A000001C6CM exists in the org. You can do that by just entering /a007A000001C6CM in the URL. Generally it is risky to include explicit ID values in tests: better that the test inserts all the data it needs. (Though as you are not using SeeAllData=true I'm not sure what is actually going on here.)
    – Keith C
    Commented Nov 28, 2015 at 10:47
  • @KeithC it doesnt exist in ORG but exist in Sandbox because I need to create a test. The rest of the data are random. I know man I'm getting kinda pissed on this code. I'm trying to retype the code and see what happens. Also, I'm adding all the Profiles and dependencies in the inbound change set. Do I need or have to upload the TestClass? thanks for your input
    – Ninja King
    Commented Nov 28, 2015 at 11:04
  • Yes you need to push both the controller and its test. A test should be designed to work in any org: the main thing that looks wrong to me is using a007A000001C6CM rather than inserting an object of whatever type that is and using the ID from the inserted object in the text.
    – Keith C
    Commented Nov 28, 2015 at 12:41
  • 1
    @NinjaKing - As Keith said, you need to create the property__c record in your test method and use the ID from that. I bet if you look at the current code coverage in production you will see that your catch block is getting covered since the ID of the record you hardcoded does not exist.
    – Eric
    Commented Nov 28, 2015 at 17:30

2 Answers 2

0

I guess there's a big change in the 2015 update when it comes to code. So in a nutshell, I recoded and made it work.

@isTest
public class ControllerProposalConvertViewTest4

{

static testMethod void convert()

{

    Property__c pro = new Property__c();

    insert pro;

    Proposal__c p = new Proposal__c (
        Name = 'Test Name',
        Already_Converted__c = 'Converted',
        Property__c = pro.id ,
        Zoning__c = 'Zone',
        Lease_Type__c = 'Monthly',
        Lease_Notes__c = 'Ground Lease',
        Lot_Size__c = 100.00,
        Lot__c = 50.00,
        Cap_Rate__c = 512.75,
        Year_Built__c = '1975',
        Term__c = 456,
        NOI__c = 123.12,
        Years_Remaining__c = 10.50,
        Lease_Commencement_Date__c = date.Today(),
        Rent_Commencement_Date__c = date.Today(),
        Lease_Expiration_Date__c = date.Today(),
        Square_Footage__c = 50000 ) ;

    insert p;

        PageReference pageRef = Page.ProposalConvertView;
        Test.setCurrentPage(pageRef);
        ApexPages.currentPage().getParameters().put('id',p.Id);
        ApexPages.StandardController std = new ApexPages.StandardController(new Proposal__c());
        ControllerProposalConvertView cont = new ControllerProposalConvertView (std);
        cont.convert();
        cont.back();
}


   static testMethod void converttwo()

   {

        Property__c pro = new Property__c();

        insert pro;

        Proposal__c p1 = new Proposal__c (
        Name = 'Test Name',
        Already_Converted__c = 'Not Converted',
        Property__c = pro.id ,
        Zoning__c = 'Zone',
        Lease_Type__c = 'Monthly',
        Lease_Notes__c = 'Ground Lease',
        Lot_Size__c = 100.00,
        Lot__c = 50.00,
        Cap_Rate__c = 512.75,
        Year_Built__c = '1975',
        Term__c = 456,
        NOI__c = 123.12,
        Years_Remaining__c = 10.50,
        Lease_Commencement_Date__c = date.Today(),
        Rent_Commencement_Date__c = date.Today(),
        Lease_Expiration_Date__c = date.Today(),
        Square_Footage__c = 50000);

        insert p1;

        PageReference pageRef = Page.ProposalConvertView;
        Test.setCurrentPage(pageRef);
        ApexPages.currentPage().getParameters().put('id',p1.Id);
        ApexPages.StandardController std = new ApexPages.StandardController(new Proposal__c());
        ControllerProposalConvertView cont = new     ControllerProposalConvertView (std);
        cont.convert();
        cont.back();

}

}

1
  • 2
    There was nt change in any update that would have caused the issue you were seeing. Hardcoded ID's were never a good practice and simply do not exist in both systems for the most part. Glad you got it working though. But you still have a lot of work to do, all you have done ins write an execution class. there is no testing going on i it
    – Eric
    Commented Nov 30, 2015 at 6:59
-1

its me again!

Few things to notice.

  1. It doesn't matter if you use SeeAllData=true or not, until & unless you use that hard coded property__c field anywhere in your code, its going to work. I tested it in my org.

  2. Since you are getting 93% coverage for the controller in your sandbox so your test class is fine(not ideal, but fine).

  3. But since the the coverage falls when you try to deploy, check if you are pushing something else(any apex class, trigger) with this controller, test class & pages.

  4. For best case and to be utter sure about this test class, just create a Property__c record at the beginning of your test Methods and use that record ID in property__c field in Proposal__c record.

Hope this will get closed this time!

5
  • #1 - It DOES matter, #2 - Test class is not fine, no asserts, nothing. It is just a code execution routine. 3 - Not relevant, 4 - Not "to be sure". The issue IS the fact of the hardcoded ID.
    – Eric
    Commented Nov 29, 2015 at 8:04
  • #1 - I tried inserting an Opportunity in a test class with arbitrary Account Id and it got saved without SeeAllData=true but it could not show the account name as the account was not valid. This is the reason I said that SeeAllData doesn't matter in this case. Commented Nov 30, 2015 at 7:07
  • #2 - Right, completely agree with you. Its not an ideal test class. Commented Nov 30, 2015 at 7:07
  • #3. it was a random guess! May be I am wrong, I am just learning. Commented Nov 30, 2015 at 7:08
  • #4. Yes, he needs to create a Property__C record instead of using a hard coded Id. Agreed! Commented Nov 30, 2015 at 7:08

You must log in to answer this question.

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