1

I am trying to write a apex test class for the following Custom Controller and am just having a tough time getting it covered over 37%. Please Help! See my code below. Thanks in advance! I am including the VF Component as they may help with writing the test class. Thanks.

Custom Class:

public with sharing class SubjectInfoController {
    public SubjectInfoController() {
        recordID = ApexPages.CurrentPage().getparameters().get('id');
    }
    public Subject__c[] sub1 {
        get {
                Subject__c[] subjectList = 
                    [SELECT Id, Name, 
                        (SELECT Residence_Street__c, Residence_City__c, Residence_State_Province__c, 
                         Residence_Country__c, Residence_Zip_Postal_Code__c from Home_Addresses__r)
                    FROM Subject__c
                    WHERE Id = :recordID];

            return subjectList;
        }
        set;
    }
    Public string recordId {get;set;}
}

Test Class:

@isTest
public class SubjectInfoControllerTest {
    public static testmethod void doTest () {
        Subject__c b = new Subject__c(First_Name__c = 'TestFirst',
            Last_Name__c = 'TestLast');
        insert b;

        Residence__c ba = new Residence__c(Residence_Street__c = '123 Test St', 
            Residence_City__c = 'Beaverton', 
            Residence_State_Province__c = 'Oregon', 
            Residence_Country__c = 'UNITED STATES', 
            Residence_Zip_Postal_Code__c  = '97006',
            Subject__c = b.id);
        insert ba;

        SubjectInfoController cont = new SubjectInfoController();
        cont.recordid = b.id;
        System.assertEquals(cont.recordid, b.id);
    }
}

VF Component:

<apex:component controller="BusinessInfoController" access="global">
<apex:attribute id="recordID" assignTo="{!recordID}" name="BusinessIDFromEmail" type="Id" description="The Business's SF Id"/>
    <table  border="0">
    <apex:repeat value="{!bus1}" var="b1">
        <apex:repeat value="{!b1.BPR_Business_Addresses__r}" var="addr1">
        <tr>
            <td>Street Address</td>
            <td><apex:outputText value="{!addr1.Street__c}"></apex:outputText></td>
        </tr>
        <tr>
            <td>City</td>
            <td><apex:outputText value="{!addr1.Business_City__c}"></apex:outputText></td>
        </tr>
        <tr>
            <td>State/Province</td>
            <td><apex:outputText value="{!addr1.State_Province__c}"></apex:outputText></td>
        </tr>
        <tr>    
            <td>Country</td>
            <td><apex:outputText value="{!addr1.Country__c}"></apex:outputText></td>
        </tr>
        <tr>    
            <td>Zip Code</td>
            <td><apex:outputText value="{!addr1.Zip_Postal_Code__c}"></apex:outputText></td>
        </tr>
        </apex:repeat>
    </apex:repeat>
    </table>
</apex:component>
3
  • Hi Nathan, welcome to SFSE. People may be more willing to help you if you post the areas that are currently covered and the areas that aren't (can be seen in Developer Console). – Brian Mansfield Jun 10 '15 at 17:46
  • 1
    insert ba; Test.setCurrentPage(Page.YOUR_PAGE); ApexPages.CurrentPage().getparameters().put('id',b.Id); You also need to pass Id in the page parameters. – the_phantom Jun 10 '15 at 17:50
  • you need to have a line Subject__c[] subjList = cont.sub1(); - this invokes the getter on sub1 – cropredy Jun 10 '15 at 23:26
0

For getting the code coverage you have to insert both objects fields. (Home_Address__c and Subject__c) .Try the below mentioned test class

@isTest
public class SubjectInfoControllerTest {
    public static testmethod void doTest () {
        Subject__c b = new Subject__c(First_Name__c = 'TestFirst',
            Last_Name__c = 'TestLast');
        insert b;

        Residence__c ba = new Residence__c(Residence_Street__c = '123 Test St', 
            Residence_City__c = 'Beaverton', 
            Residence_State_Province__c = 'Oregon', 
            Residence_Country__c = 'UNITED STATES', 
            Residence_Zip_Postal_Code__c  = '97006',
            Subject__c = b.id);
        insert ba;

 Home_Address__c homeobject = new Home_Address__c();
 // insert all the required fields of Home_Address__c object.
 insert homeobject;

        SubjectInfoController cont = new SubjectInfoController();
        cont.recordid = b.id;
        System.assertEquals(cont.recordid, b.id);
    }
}

Let me know if this helps.

0

In support to the Heisenberg comment I am providing this link where you can get more idea of writing test class for the vf page.

Please refer the following URL.

Let me add some more tips.

When you are writing the test class for vf page

1) Initate the Page as said by Heisenberg in the comment

2) create the required test date for the Vf page and the methods in the controller

3) create an instance for the controller

4) keep calling the methods in the controller in your test methods

Refer this URL. https://developer.salesforce.com/forums/?id=906F0000000914yIAA

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.