0

I am new to salesforce and i am writing a test class for custom controller and it is showing 64% code coverage Here is my visual force page in which that controller is used

<apex:page standardcontroller="Property__c" extensions="propertycontrollerdetail" standardstylesheets="true" showheader="false" sidebar="false" >
    <head>
            <apex:stylesheet value="{!URLFOR($Resource.Bootstrap,'bootstrap-3.3.5-dist/css/bootstrap.min.css')}"/>
            <apex:includeScript value="{!URLFOR($Resource.Bootstrap,'bootstrap-3.3.5-dist/js/bootstrap.min.js')}" />

    </head>
    <body>
         <nav class="navbar navbar-default">

  <div class="container-fluid">

  <div class="navbar-header">
      <apex:image url="{!URLFOR($Resource.Images, 'Images/RE.jpg')}" width="1340" height="130" />
          </div>

    <div class="navbar-header">
          <a class="navbar-brand" href="https://itroos-dev-ed--seit.na34.visual.force.com/apex/HomePage?core.apexpages.request.devconsole=1">Home</a> 
    </div>

    <div class="navbar-header">
      <a class="navbar-brand" href="#">Property</a>
    </div>
    <div>
      <ul class="nav navbar-nav">
        <li><a href="https://itroos-dev-ed--seit.na34.visual.force.com/apex/Property">Detail</a></li>
        <li class="active"><a href="https://itroos-dev-ed--seit.na34.visual.force.com/apex/AgentCreate?core.apexpages.request.devconsole=1">Create</a></li>
      </ul>
    </div>
  </div>
</nav>
        <apex:messages />
        <!-- <apex:sectionheader title="{!$ObjectType.Property__c.label} Edit" subtitle="{!IF(ISNULL(Property__c.Name), 'New Property',Property__c.Name)}"/> -->
    <apex:form >

        <apex:pageblock mode="edit" title="{!$ObjectType.Property__c.label} Edit">
            <apex:pageblockbuttons >
                <apex:commandbutton value="Save" action="{!SaveA}"/>

            </apex:pageblockbuttons>

            <!-- **********   [Record Type : Master ]   **********  -->
            <apex:outputpanel >
                <apex:pageblocksection title="Information" showheader="true" columns="2">
                    <apex:outputfield value="{!Property__c.Name}"/>
                    <apex:outputfield value="{!Property__c.OwnerId}"/>
                    <apex:inputfield value="{!Property__c.Address__c}" required="false"/>
                    <apex:pageblocksectionitem />
                    <apex:inputfield value="{!Property__c.Type__c}" required="false"/>
                    <apex:pageblocksectionitem />
                    <apex:inputfield value="{!Property__c.Status__c}" required="false"/>
                    <apex:pageblocksectionitem />
                    <apex:inputfield value="{!Property__c.Size__c}" required="false"/>
                    <apex:pageblocksectionitem />
                    <apex:inputfield value="{!Property__c.Size_in_sq_ft__c}" required="false"/>
                    <apex:pageblocksectionitem />
                    <apex:inputfield value="{!Property__c.Min_Price__c}" required="false"/>
                    <apex:pageblocksectionitem />
                    <apex:inputfield value="{!Property__c.Max_Price__c}" required="false"/>
                    <apex:pageblocksectionitem />
                    <apex:inputfield value="{!Property__c.Description__c}" required="false"/>
                    <apex:pageblocksectionitem />
                    <apex:pageblocksectionitem >
                        <apex:outputlabel value="Location"/>
                        <apex:outputpanel >
                            <apex:inputfield value="{!Property__c.Location__Latitude__s}" required="false"/>
                            <apex:inputfield value="{!Property__c.Location__Longitude__s}" required="false"/>
                        </apex:outputpanel>
                    </apex:pageblocksectionitem>
                    <apex:pageblocksectionitem />
                    <apex:inputfield value="{!Property__c.Agent_ID__c}" required="false"/>
                    <apex:pageblocksectionitem />
                </apex:pageblocksection>
            </apex:outputpanel>
        </apex:pageblock>
    </apex:form>

    </body>

</apex:page>

Controller is

public with sharing class propertycontrollerdetail {
public Property__c ppo {get; set;}


public propertycontrollerdetail(ApexPages.StandardController controller)
{
   ppo = (Property__c)controller.getRecord();
}

public pageReference SaveA()
{
    try{
     upsert ppo;
    PageReference pageRef = new PageReference('https://itroos-dev-ed--seit.na34.visual.force.com/apex/Property');
    pageRef.setRedirect(true);
    return pageRef;  
    }
    catch(Exception ex)
    {
       ApexPages.addMessages(ex);
     system.debug(ex);
     PageReference pageRef = new PageReference('https://itroos-dev-ed.my.salesforce.com/a01/o');
    pageRef.setRedirect(true);
    return pageRef; 
    }
 }
}

And my test class is

@isTest
public class TestPropertycontrollerdetail {
    static testMethod void saveA(){

        Property__c pro = new Property__c( seit__Address__c = 'Lalazkhbhjkgar',seit__Description__c='sghj sjgh ',seit__Size__c='10 Marla',seit__Size_in_sq_ft__c='12000',seit__Status__c='Sold',seit__Type__c='Residential');


    ApexPages.StandardController sc = new ApexPages.standardController(pro);

    propertycontrollerdetail controller = new propertycontrollerdetail(sc);
        controller.SaveA();
        //add your asserts

    }
    static testMethod void saveA1(){

       Property__c pro = new Property__c();
       ApexPages.StandardController sc = new ApexPages.standardController(pro);
       propertycontrollerdetail controller = new propertycontrollerdetail(sc);
       controller.SaveA();
       //add your asserts
    }
}

If any one could guide me or help me with the code i will appreciate it

  • turn off store only aggregate code coverage. lots of topics on this here. If you are looking in dev console see this answer: salesforce.stackexchange.com/questions/97314/… – Eric Dec 29 '15 at 5:41
  • thanks Ratan it worked but it is covering only 21% how i can increase the code coverage – Sheheri Dec 29 '15 at 5:50
  • @Sheheri you need to call SaveA method( i.e. controller.SaveA()) and check your expected result. – Ratan Paul Dec 29 '15 at 5:52
0
@isTest
public class TestPropertyController{

    static testMethod void saveA(){

        Property__c pro = new Property__c( seit__Address__c = 'Lalazkhbhjkgar',seit__Description__c='sghj sjgh ',seit__Size__c='10 Marla',seit__Size_in_sq_ft__c='12000',seit__Status__c='Sold',seit__Type__c='Residential');
        try
        {
        insert pro;
            System.debug('Insert Successfull');
        }
        catch(Exception ex)
        {
            System.debug(ex.getMessage());
        }

    ApexPages.StandardController sc = new ApexPages.standardController(pro);

    propertycontrollerdetail controller = new propertycontrollerdetail(sc);
    controller.SaveA();
    // add your asserts to check expected behavior 
    }
}

Edit

Controller

public with sharing class propertycontrollerdetail {
public Property__c ppo {get; set;}


    public propertycontrollerdetail(ApexPages.StandardController controller)
    {
       ppo = (Property__c)controller.getRecord();
    }

    public pageReference SaveA()
    {
        try{
         upsert ppo;
        PageReference pageRef = new PageReference('https://itroos-dev-ed--seit.na34.visual.force.com/apex/Property');
        pageRef.setRedirect(true);
        return pageRef;  
        }
        catch(Exception ex)
        {
           ApexPages.addMessages(ex);
         system.debug(ex);
         PageReference pageRef = new PageReference('https://itroos-dev-ed.my.salesforce.com/a01/o');
        pageRef.setRedirect(true);
        return pageRef; 
        }
    }
}

Test Class

@isTest
public class TestPropertycontrollerdetail {
    static testMethod void saveA(){

        Property__c pro = new Property__c( seit__Address__c = 'Lalazkhbhjkgar',seit__Description__c='sghj sjgh ',seit__Size__c='10 Marla',seit__Size_in_sq_ft__c='12000',seit__Status__c='Sold',seit__Type__c='Residential');


    ApexPages.StandardController sc = new ApexPages.standardController(pro);

    propertycontrollerdetail controller = new propertycontrollerdetail(sc);
        controller.SaveA();
        //add your asserts

    }
    static testMethod void saveA1(){

       Property__c pro = new Property__c();
       ApexPages.StandardController sc = new ApexPages.standardController(pro);
       propertycontrollerdetail controller = new propertycontrollerdetail(sc);
       controller.SaveA();
       //add your asserts
    }

}
  • its working but code coverage is only 60% – Sheheri Dec 29 '15 at 6:01
  • insert one more Property record by missing any required field, then it goes into catch block. it will increase code coverage – Sunny Dec 29 '15 at 6:06
  • @Sheheri why you need throw ex; inside SaveA method? – Ratan Paul Dec 29 '15 at 6:13
  • I have removed it that now..... – Sheheri Dec 29 '15 at 6:20
  • @Ratan it still not covering code – Sheheri Dec 29 '15 at 6:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.