0

I've got a Visualforce component with a custom controller that I inherited and need to write a test class for. I am having difficulty getting the one method it contains to execute. I have called the VF page containing the component, but I can't work out how to make it then call the component to test the controller's behaviour.

This previous question and answer gave me some guidance, but I am still struggling with it.

Controller

public with sharing class TypeControllerNew2 {

    public class CaseCharges {

        public Integer count {get; set;}
        public String typ {get; set; }
        CaseCharges(String typ, Integer count) {
            this.count = count;
            this.typ = typ;
        }
    }
}

VF Component

<apex:page docType="html-5.0" showHeader="false" sidebar="false" cache="false" controller="TypeControllerNew2" standardStylesheets="false" >

<body>

  <outputPanel style="padding: 15px;">
      <a href="/apex/CaseList?fcf=00B21000000UI5I" class="alert-link">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Estimate of Non-Primary Charges</a>

           <apex:chart height="380" width="400" data="{!CaseCharges}">
           <apex:legend position="right"/>
           <apex:axis type="Numeric" position="bottom" fields="count" title="Case Record Count" minimum="0" maximum="10" />
          <apex:axis type="Category" position="left" fields="typ" title="Case Type" />
          <apex:barSeries title="In Progress Cases" orientation="horizontal" gutter="10" axis="bottom" xField="count" yField="typ" colorsProgressWithinSeries="true" colorset="red, green, yellow, black, white, orange" >
         <apex:chartTips height="20" width="120" />
         </apex:barSeries>
         </apex:chart>
            <apex:dataTable value="{!CaseCharges}" var="ty">
            <apex:column headerValue="Case Type" value="{!ty.typ}"/>
            <apex:column headerValue="Number of Cases" value="{!ty.count}"/>
         </apex:dataTable>
     </outputPanel>
  </body>

Test class

@isTest (seeAllData = true)
public class TypeControllerNew2_Test {
    static testMethod void typeController2Test(){

        PageReference graph2Page = Page.AWSHome;
        Test.setCurrentPage(graph2Page);

        TypeControllerNew2 tcNew2Test = new TypeControllerNew2();
        tcNew2Test.getCaseCharges();
    }
}

enter image description here

1 Answer 1

1

Looks to me like all you need to do is instanciate that inner Class in your test method, but first you will have to make your constructor visible to the test Method by annotating it with an @testVisible annotation. (you could also make it public)

.
.
.
@testVisible CaseCharges(String typ, Integer count) {
    this.count = count;
    this.typ = typ;
}
.
.

With that done, you can now instanciate your inner Class, get code coverage, and ensure that your constructor works as required.

@isTest
public class TypeControllerNew2_Test {
    static testMethod void typeController2Test(){

        //Don't really need these 2 lines to test the controller...
        PageReference graph2Page = Page.AWSHome;
        Test.setCurrentPage(graph2Page);

        TypeControllerNew2 tcNew2Test = new TypeControllerNew2();

        //create an instance of that inner Class...
        TypeControllerNew2.CaseCharges myInnerClass = new TypeControllerNew2.CaseCharges('A string for typ', 2);
        //that should cover that constructor!
        //Now make some assertions against your var...
        system.assertEquals('A string for typ', myInnerClass.typ);
        system.assertEquals(2, myInnerClass.count);
    }
}

Also PLEASE take that @isTest(seeAllData = true) out of your test Class. It doesn't look like you need it and goes against best practice.

3
  • Ok removed the seeAllData but still getting an error 'variable does not exist: myInnerClass'
    – Dave Humm
    Oct 28, 2016 at 20:14
  • still need to be able to instantiate the inner class as its throwing an error of 'variable does not exist:myInnerClass' when I try to make those changes.
    – Dave Humm
    Oct 28, 2016 at 20:34
  • Thanks, I'd realised that constructor needed to be public so the test could instantiate it.
    – Dave Humm
    Oct 28, 2016 at 20:59

You must log in to answer this question.

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