+ Start a Discussion
Daniel B ProbertDaniel B Probert 

Component Controller Test Class help

Hi,

I have a VF page that is loaded on my contact page:

<apex:page showHeader="false" sidebar="false" standardController="Contact" extensions="GivingSummaryContactExtension">

	<c:GivingSummary SourceObject="{!objId}" FilterField="Donor__c" />
	
</apex:page>
the extensions for this is:

public with sharing class GivingSummaryContactExtension {
  public Id objId {get; set;}

  public GivingSummaryContactExtension(ApexPages.StandardController con) {
    objId = con.getId();
  }
}

the page you'll notice calls a custom component code here:

<apex:component controller="GivingSummaryComponentController">

	<apex:attribute name="SourceObject" type="String" assignTo="{!ObjectId}" description="Contact or Account Id"/>
	<apex:attribute name="FilterField" type="String" assignTo="{!FieldName}" description="Field on donation to filter against"/>

	<apex:pageBlock >
		<apex:pageBlockTable value="{!Values}" var="item">
			<apex:column value="{!Values[item].DonationType}" headerValue="Donation Type"/> 
			<apex:column headerValue="Total">
				<apex:outputText value="{!CurrencySymbol}{!Values[item].Total}" />
			</apex:column> 
			<apex:column headerValue="Year To Date">
				<apex:outputText value="{!CurrencySymbol}{!Values[item].YearToDate}" />
			</apex:column> 
			<apex:column headerValue="Last Year">
				<apex:outputText value="{!CurrencySymbol}{!Values[item].LastYear}" />
			</apex:column> 
		</apex:PageBlockTable>
	</apex:pageBlock>

this is the bit that i haven't been able to figure out how to test - this controller:

public with sharing class GivingSummaryComponentController {

  public String FieldName {get; set;}
  public String ObjectId {get; set;}

  private Map<Id, GivingAmounts> theValues;

  public GivingSummaryComponentController() {


  }

  private string getISOCOde(){

    String qry = 'SELECT CurrencyIsoCode FROM ' + ((Id)ObjectId).getSObjectType().getDescribe().getName() + ' WHERE Id = \'' + objectId + '\'';
    List<sObject> objs = database.query(qry);

    if(!objs.isEmpty())
      return (String)objs[0].get('CurrencyIsoCode');
    else
      return 'GBP';
  }

  public String getCurrencySymbol(){

    String code = getISOCode();
    String currencySymbol = 'x';
    if(code == 'GBP')
      currencySymbol = '£';
    else if(code == 'USD')
      currencySymbol = '$';
    

    return currencySymbol;
  }

  public Map<Id, GivingAmounts> getValues(){

    if(theValues == null){
      theValues = new Map<Id, GivingAmounts>();
      for(RecordType rt : [SELECT Id, Name FROM RecordType WHERE SObjectType = 'Opportunity'])
        theValues.put(rt.Id, new GivingAmounts(rt.Name));
    }else 
      return theValues;


    // Total
    String qry = 'SELECT RecordTypeId, Amount FROM Opportunity WHERE IsWon = TRUE AND ' + FieldName + ' = \'' + ObjectId + '\'';
    for(Opportunity opp : database.query(qry))
      theValues.get(opp.RecordTypeId).Total += opp.Amount;

    // YTD
    qry = 'SELECT RecordTypeId, Amount FROM Opportunity WHERE CloseDate = THIS_YEAR AND IsWon = TRUE AND ' + FieldName + ' = \'' + ObjectId + '\'';
    for(Opportunity opp : database.query(qry))
      theValues.get(opp.RecordTypeId).YearToDate += opp.Amount;
    
    // Last Year
    qry = 'SELECT RecordTypeId, Amount FROM Opportunity WHERE CloseDate = LAST_YEAR AND IsWon = TRUE AND ' + FieldName + ' = \'' + ObjectId + '\'';
    for(Opportunity opp : database.query(qry))
      theValues.get(opp.RecordTypeId).LastYear += opp.Amount;

    return theValues;
  }

  public class GivingAmounts{

    public String DonationType {get; set;}
    public Decimal YearToDate {get; set;}
    public Decimal LastYear {get; set;}
    public Decimal Total {get; set;}

    public GivingAmounts(String name){
      this.DonationType = name;
      this.YearToDate = 0.00;
      this.LastYear = 0.00;
      this.Total = 0.00;
    }

  }
}

i've got a test that is 100% for the extensions but I can only get 9% on the component controller - this is what i've got so far - any ideas?

@isTest
public class Test_GivingSummary{
    public static testMethod void Contact(){
        ID HAcc = [select Id from RecordType where Name = 'Household' and SobjectType = 'Account'].id;
        ID HCon = [select Id from RecordType where Name = 'Household Contact' and SobjectType = 'contact'].id;
        Account acc = new Account(RecordTypeID=HAcc,Name='Tests');
        insert acc;
        Contact con = new Contact(RecordTypeID=HCon,LastName='Demo',FirstName='Demo',AccountID=acc.id);
        insert con;
        Campaign camp = new Campaign(Name='Demo');
        insert camp;
        Opportunity opp = new Opportunity(Name='DEMO',Amount=10.00,CurrencyISOCode='GBP',Donor__c=con.id,AccountID=acc.id,CampaignID=camp.id,StageName='Received',CloseDate=system.today());
        insert opp;
        
        ApexPages.Standardcontroller sc1 = New ApexPages.StandardController(con);
        GivingSummaryContactExtension   ext = new GivingSummaryContactExtension(sc1 );
        PageReference pageRef = Page.GivingSummary;
        Test.setCurrentPageReference(pageRef);

    

    }
   
    public static testMethod void ContactAccountController(){
        ID HAcc = [select Id from RecordType where Name = 'Household' and SobjectType = 'Account'].id;
        ID HCon = [select Id from RecordType where Name = 'Household Contact' and SobjectType = 'contact'].id;
        Account acc = new Account(RecordTypeID=HAcc,Name='Tests');
        insert acc;
        Contact con = new Contact(RecordTypeID=HCon,LastName='Demo',FirstName='Demo',AccountID=acc.id);
        insert con;
        Campaign camp = new Campaign(Name='Demo');
        insert camp;
        Opportunity opp = new Opportunity(Name='DEMO',Amount=10.00,CurrencyISOCode='GBP',Donor__c=con.id,AccountID=acc.id,CampaignID=camp.id,StageName='Received',CloseDate=system.today());
        insert opp;
        


        GivingSummaryComponentController myACController = new GivingSummaryComponentController();
        
        
        PageReference myPage = new Pagereference('/apex/GivingSummaryContact');
        Test.setCurrentPage(myPage);        


        
        

    }
}

any one able to point me in the right direction for getting closer to 100% on the controller.

cheers
dan

Shashikant SharmaShashikant Sharma
Hi,

For the coverage purpose you could directly call your public methods from this
GivingSummaryComponentController myACController = new GivingSummaryComponentController();

like 
myACController.getCurrencySymbol()

similare for getValues

You could create appropriate data in different test methods to satisfy the conditions inside to make sure it goes in every part of of these methods.

Let me know if still have any questions.


Daniel B ProbertDaniel B Probert
hi cheers for getting back to me i'd added those in to it's own method:

public static testMethod void ContactAccountController(){
        ID HAcc = [select Id from RecordType where Name = 'Household' and SobjectType = 'Account'].id;
        ID HCon = [select Id from RecordType where Name = 'Household Contact' and SobjectType = 'contact'].id;
        Account acc = new Account(RecordTypeID=HAcc,Name='Tests');
        insert acc;
        Contact con = new Contact(RecordTypeID=HCon,LastName='Demo',FirstName='Demo',AccountID=acc.id);
        insert con;
        Campaign camp = new Campaign(Name='Demo');
        insert camp;
        Opportunity opp = new Opportunity(Name='DEMO',Amount=10.00,CurrencyISOCode='GBP',Donor__c=con.id,AccountID=acc.id,CampaignID=camp.id,StageName='Received',CloseDate=system.today());
        insert opp;
        

        GivingSummaryComponentController myACController = new GivingSummaryComponentController();
        myACController.getCurrencySymbol();
        myACController.getValues();
    }
when i run the test to check coverage my coverage has indeed gone up from 9% to 18% the problem is that it is erroring out with:

System.NullPointerException: Attempt to de-reference a null object
Class.GivingSummaryComponentController.getISOCOde: line 15, column 1
Class.GivingSummaryComponentController.getCurrencySymbol: line 26, column 1
Class.Test_GivingSummary.ContactAccountController: line 57, column 1
what i think i need to figure out is how I can load a contact into the controller so that the data in the method is loaded but that's what I'm missing - i think.

cheers
dan