+ Start a Discussion
Mike ArthurMike Arthur 

Unit test compile error when trying to reference method in controller class

I have a custom controller that I'm trying to write a unit test for.
In the unit test I'm trying to call the method in the controller that does the work - it runs a SOQL query to produce a list via a wrapper class on a vf page.
The compile error is 'Method does not exist or incorrect signature: SummariseQuotedWorks.getWISumOut()'.
The error is on the line 'ResultList = SummariseQuotedWorks.getWISumOut();'.
I think I just need a pointer on the syntax due to the added complexity of the method being tested returning a custom object.
Here's the controller class:
public class SummariseQuotedWorks {
    private final Quote qte;
    
    public SummariseQuotedWorks(ApexPages.StandardController controller) {
        this.qte = (Quote)controller.getRecord();
        }

/* Use a wrapper class to store the aggregate values in a way that is accessible to Visualforce */

public class WISum{
        public String Acct {get; set;}
        public String WorkItem {get; set;}
        public Decimal TotalCost {get; set;}
        public WISum(string a,string w,decimal c){
                this.Acct=a;
                this.WorkItem=w;
                this.TotalCost=c;
        }
}


public List<WISum> WISumList = new List<WISum>();

public List<WISum> getWISumOut(){
system.debug('IAmHere');

		String theAcct = '';

system.debug('1: qte: '+qte.id);

        AggregateResult[] AgR = [SELECT Account__r.Name AC, WorkItem__r.Name WI, sum(cost1__c) TC FROM Quoted_Work__c
where Quote__c=:qte.Id
group by rollup(Account__r.Name, WorkItem__r.Name)];

system.debug('QW AgR size: '+AgR.size());

        for (AggregateResult WIRecs : AgR) {


       
 /*      WISumList.add(new WISum((String) WIRecs.get('AC'), (String) WIRecs.get('WI'), (Decimal) WIRecs.get('TC'))); */
 	        WISumList.add(new WISum((String) ((WIRecs.get('AC') == theAcct) ? '' : WIRecs.get('AC')), (String) (WIRecs.get('WI') == null && WIRecs.get('AC') == null ? 'Grand Total' : (WIRecs.get('WI') == null ? 'Subtotal' : WIRecs.get('WI'))), (Decimal) WIRecs.get('TC')));
 
        	theAcct = (String)WIRecs.get('AC');
 
        }
        return WISumList;
}
    
}

Here's the test class:
 
@isTest
private class SummariseQuotedWorksTest {
	
	@isTest static void test_method_one() {

// Create Test Data:

		RecordType art = [select id,Name from RecordType where SobjectType='Account' and Name='Head Office' Limit 1];
        Account a = new Account(
            Name='Mike',
            RecordTypeId=art.Id,
            Region__c='National',
            Status__c='Active',
        	Type='Prospect',
        	CurrencyIsoCode='GBP',
        	Phone='1',
            Sage_Acc__c='1',
            Sage_Company__c='Water Sage',
        	Division__c='Water');
        insert a;

		RecordType crt = [select id,Name from RecordType where SobjectType='Contact' and Name like '%Contact%' Limit 1];
        Contact ctct = new Contact(
            AccountId=a.Id,
            LastName='Smith',
            RecordTypeId=crt.Id,
        	CurrencyIsoCode='GBP',
        	Phone='1');
        insert ctct;

		RecordType ort = [select id,Name from RecordType where SobjectType='Opportunity' and Name like '%Project%' Limit 1];
        Opportunity opp = new Opportunity(
            Name='Mike',
            AccountId=a.Id,
            RecordTypeId=ort.Id,
        	Type='New Business',
            LeadSource='Other',
            StageName='Present Quote',
            CloseDate=Date.valueOf('2015-12-31'),
        	CurrencyIsoCode='GBP',
        	Division__c='Water');
        insert opp;

        Quote q = new Quote(
        Name='Mike',
        OpportunityId=opp.Id,
        Type__c='Project',
        Account_Manager__c='Mark Viner',
        Contact=ctct,
        Sage_Company__c='Water Sage');
        insert q;

         Consumable__c consum = new Consumable__c (
            Name='A Consumable',
            Active__c=TRUE,
            Price__c=1,
            Product_Code__c='A Code',
            Product_Description__c='A Description');
        insert consum;

        Work_Item__c w = new Work_Item__c (
            Name='A Work Item',
            Sample_Consumable__c=consum.Id);
        insert w;

        Quoted_Work__c QW = new Quoted_Work__c (
            Account__c = a.Id,
            Quote__c= q.Id,
            WorkItem__c = w.Id,
            Assigned_Role__c = 'Technician',
            Duration_hrs__c = 1,
            Quantity__c = 1,
            Frequency__c = 'Annually');
        insert QW;

        Covered_Consumable_2__c CC2 = new Covered_Consumable_2__c (
            Site__c = a.Id,
            Quote__c= q.Id,
            Consumable__c = consum.Id,
            Quantity__c = 1);
        insert CC2;

// Set current page so that controller picks up Quote Id:

        PageReference pageRef = new PageReference('/apex/worksSummary?Id='+q.Id);

        Test.setCurrentPage(pageRef);

// Instantiate a new SummariseQuotedWorks object:

        Test.startTest();
	        SummariseQuotedWorks controller = new SummariseQuotedWorks(new ApexPages.StandardController(q));

            List<WISum> ResultList = new List<WISUm>();
            ResultList = SummariseQuotedWorks.getWISumOut();
	        system.assertEquals(controller.WISumList[0].TotalCost.abs(),45);
        Test.stopTest();

	}
public class WISum{
        public String Acct {get; set;}
        public String WorkItem {get; set;}
        public Decimal TotalCost {get; set;}
    }

}

Thanks,
Mike.

 
Best Answer chosen by Mike Arthur
VolodymyrVolodymyr
You define a class WISum in your test class, but the SummariseQuotedWorks.getWISumOut() method returns List<SummariseQuotedWorks.WISum>.
This is what you need to do:
List<SummariseQuotedWorks.WISum> ResultList = controller.getWISumOut();

And probably delete the WISum class from your test class.

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi Mike,

Since you are accessing instance method of the class(controller), so you need to use object of the class to access the public members.
 
controller.getWISumOut();

Only static methods can be called by the class name.
VolodymyrVolodymyr
You define a class WISum in your test class, but the SummariseQuotedWorks.getWISumOut() method returns List<SummariseQuotedWorks.WISum>.
This is what you need to do:
List<SummariseQuotedWorks.WISum> ResultList = controller.getWISumOut();

And probably delete the WISum class from your test class.
This was selected as the best answer
Mike ArthurMike Arthur
Thank you Pankaj and Volodymyr - it's working, much appreciated.