function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hazel EganHazel Egan 

Error: Compile Error: Method does not exist or incorrect signature: [exportCSV].exportCSV() at line 16 column 9 in test class

I'm very new to Salesforce and have written a class to export an opportunity to csv. It now exports correctly but when trying to write a test class I'm getting the above error when trying to reference the method within my class. I'm also not sure what to write for the assert method. As I'm new to this I'm really lost, any help would be appreciated.

exportCSV class:

public with sharing class exportCSV {
    public string header{get;set;}
    public String currentRecordId {get;set;}
    public String rId {get;set;}
    public List<Opportunity> AllOpenOpportunities {get; private set;} 
    public exportCSV() {
        header = 'Acc_Ref__c,Purchase_Order_Number1__c,Position__c,Total_Hours_Required__c,Shift_Start_Date__c,Shift_End_Date__c,Number_of_Shifts__c,Hours_per_Shift__c,Shift_Time__c,Pay_Code__c,Opportunity_Unique_Reference__c\r';
        rId  = ApexPages.currentPage().getHeaders().get('Referer');
        Id currentRecordId = rId.right(15);
        AllOpenOpportunities = [SELECT id, Name, Acc_Ref__c, Purchase_Order_Number1__c, Position__c, Total_Hours_Required__c, Shift_Start_Date__c, Shift_End_Date__c,Number_of_Shifts__c,Hours_per_Shift__c,Shift_Time__c,Pay_Code__c, Opportunity_Unique_Reference__c
                            FROM Opportunity 
                            WHERE id =: currentRecordId];
    }
}


Test class so far:

@isTest                    
public class TestClassOne {

    public static testMethod void testMethodOne() {
        // Create Account Data
        Account acc = new Account(Name='TestAcc');
        insert acc;

        // Create Opportunity Data
        Opportunity opp = new Opportunity(Name='Test Opp', AccountId=acc.Id);
        insert opp;
        system.currentPageReference().getParameters().put('currentRecordId', opp.Id);
        exportCSV conObj = new exportCSV();
        Test.startTest();
        // Call Methods of the class
        conObj.exportCSV();
        Test.stopTest();
        // Write assert statements
    }   
}

 
Best Answer chosen by Hazel Egan
VineetKumarVineetKumar
Your rId is coming in as blank, you need to modify your code a bit.
First in your test class you need to set the value for referrer.
PageReference pageRef = Page.<NameofthePage>;     
Test.setCurrentPage(pageRef);
system.currentPageReference().getParameters().put('currentRecordId', opp.Id);
system.currentPageReference().getParameters().put('Referer', <someValue>);

And in your code put the null check
if(!String.isBlank(rId)){
    Id currentRecordId = rId.right(15);
}

All Answers

VineetKumarVineetKumar
No need for conObj.exportCSV(); on line 16.
Since exportCSV is the constructor for your controller, which will be called by deafault as soon as you initialise the variable for the class.
that is to say, the constructor will be called as soon as you do
exportCSV conObj = new exportCSV();
Hazel EganHazel Egan
Thank you for the quick response! I added in a couple bits, mostly just a couple of required fields for Opportunities and a small query to test the account. I'll add some more asserts but I tried running what I have a got an error 

System.NullPointerException: Attempt to de-reference a null object
Class.exportCSV.<init>: line 9, column 1
Class.TestClassOne.testMethodOne: line 13, column 1

This is the current test. Any help with this error would be great. Thanks for your help so far!
 
@isTest                    
public class TestClassOne {

    public static testMethod void testMethodOne() {
        // Create Account Data
        Account acc = new Account(Name='TestAcc');
        insert acc;

        // Create Opportunity Data
        Opportunity opp = new Opportunity(Name='Test Opp', AccountId=acc.Id, Location__c='Cork', StageName='Open',CloseDate=Date.Today());
        insert opp;
        system.currentPageReference().getParameters().put('currentRecordId', opp.Id);
        exportCSV conObj = new exportCSV();
        Test.startTest();
        // Call Methods of the class
        Test.stopTest();
        // Write assert statements
        List<Account> insertedAccounts = [SELECT Name
                                      FROM Account
                                      WHERE Id =: acc.id];
    }   
}

 
Hazel EganHazel Egan

Sorry should have mentioned, line 9 in exportCSV is

Id currentRecordId = rId.right(15);

line 13 in the test class is the initialisation
exportCSV conObj = new exportCSV();

 
VineetKumarVineetKumar
You have missed setting the VF page context
Soemthing like
PageReference pageRef = Page.<NameofthePage>;     
Test.setCurrentPage(pageRef);
system.currentPageReference().getParameters().put('currentRecordId', opp.Id);
Since there was no page set, the 3 line in the above code was throwing you a null pointer exception
Hazel EganHazel Egan
Thanks for your help again! Although unfortunately I'm still getting the same error :( Not sure if it helps but this is my very minimal vf page that sets up the csv
 
<apex:page controller="exportCSV" contentType="application/vnd.ms-excel#{!AllOpenOpportunities[0].Name}_{!DAY(TODAY())}.{!MONTH(TODAY())}.{!YEAR(TODAY())}.csv"><apex:repeat value="{!AllOpenOpportunities}" var="eachOpportunity">{!header}
    {!eachOpportunity.Acc_Ref__c},{!eachOpportunity.Purchase_Order_Number1__c},{!eachOpportunity.Position__c},{!eachOpportunity.Total_Hours_Required__c},{!eachOpportunity.Shift_Start_Date__c},{!eachOpportunity.Shift_End_Date__c},{!eachOpportunity.Number_of_Shifts__c},{!eachOpportunity.Hours_per_Shift__c},{!eachOpportunity.Shift_Time__c},{!eachOpportunity.Pay_Code__c},{!eachOpportunity.Opportunity_Unique_Reference__c}
    </apex:repeat>
</apex:page>

 
VineetKumarVineetKumar
What is the error? Same error?
Hazel EganHazel Egan
Yes same error in on the same line unfortunately
VineetKumarVineetKumar
Your rId is coming in as blank, you need to modify your code a bit.
First in your test class you need to set the value for referrer.
PageReference pageRef = Page.<NameofthePage>;     
Test.setCurrentPage(pageRef);
system.currentPageReference().getParameters().put('currentRecordId', opp.Id);
system.currentPageReference().getParameters().put('Referer', <someValue>);

And in your code put the null check
if(!String.isBlank(rId)){
    Id currentRecordId = rId.right(15);
}
This was selected as the best answer
Hazel EganHazel Egan

This seemed like the way to go but now when I try to export the csv through the button (which was working before), the page is showing this

Subscript is invalid because list is empty
Error is in expression 'application/vnd.ms-excel{!'#'}{!AllOpenOpportunities[0].Name}_{!DAY(TODAY())}.{!MONTH(TODAY())}.{!YEAR(TODAY())}.csv' in component <apex:page> in page exporttocsv

However when I run a test with the new changes it passes.

It seems once I added in the check to make sure rId is not empty I'm now getting this error. The csv export works without the check for the empty string, though the test will fail.

VineetKumarVineetKumar
This is some other error reported on your page.
What is this script doing on the page?
No need to specify
#{!AllOpenOpportunities[0].Name}_{!DAY(TODAY())}.{!MONTH(TODAY())}.{!YEAR(TODAY())}.csv">
cValid value for content type is just
application/vnd.ms-excel
Hazel EganHazel Egan

Everything after the # is the name of the csv. What I was trying to do was to use the name of the first opportunity in the AllOpenOpportunities list, and then todays date.

So if the opportunity name field was "Additional Services Example" then the exported csv was called:

Additional Services Example - 30/08/2016.csv 

You are correct in saying this is the issue, if i were to put "#test.csv" it exports correctly. I think my issue is in trying to reference the first opportunity in the list and then retrieve the .Name field of that opportunity.

Thank you so much for all your help so far, think this is the final issue!

Hazel EganHazel Egan
Oh wait, I just checked the exported file and now there is nothing in it.
Hazel EganHazel Egan
After testing, without the isBlank check the exported csv has all the correct data, however, with the isBlank check the csv is completely empty. Not even the header is in the file, which it should be even if nothing is returned by the query
Hazel EganHazel Egan
Okay. So I've solved the issues with my code and the test runs successfully. I'm not sure what I was trying to do previously but it seems I was over complicating the code. The name of the csv was not the issue it was that my query was returning a blank string. I've posted my final class below, I'm still using the same vf page. I've also posted the most up to date test. It's covering 75% of my code. Would you have any suggestions as to how to bring this coverage up a bit?

exportCSV Class:
public with sharing class exportCSV {
    public string header{get;set;}
    public String currentRecordId {get;set;}
    public String rId {get;set;}
    public String test {get;set;}
    public List<Opportunity> AllOpenOpportunities {get; private set;} 
    public exportCSV() {
        header = 'Acc_Ref__c,Purchase_Order_Number1__c,Position__c,Total_Hours_Required__c,Shift_Start_Date__c,Shift_End_Date__c,Number_of_Shifts__c,Hours_per_Shift__c,Shift_Time__c,Pay_Code__c,Opportunity_Unique_Reference__c\r';
        currentRecordId  = ApexPages.currentPage().getHeaders().get('referer');
        if(!String.isBlank(currentRecordId)){
            currentRecordId = currentRecordId.right(15);
        }
        AllOpenOpportunities = [SELECT id, Name, Acc_Ref__c, Purchase_Order_Number1__c, Position__c, Total_Hours_Required__c, Shift_Start_Date__c, Shift_End_Date__c,Number_of_Shifts__c,Hours_per_Shift__c,Shift_Time__c,Pay_Code__c, Opportunity_Unique_Reference__c
                            FROM Opportunity 
                            WHERE id =: currentRecordId];
    }
}

Test Class:
@isTest                    
public class TestClassOne {

    public static testMethod void testMethodOne() {
        // Create Account Data
        Account acc = new Account(Name='TestAcc');
        insert acc;

        // Create Opportunity Data
        Opportunity opp = new Opportunity(Name='Test Opp', AccountId=acc.Id, Location__c='Cork', StageName='Open',CloseDate=Date.Today());
        insert opp;
        PageReference pageRef = Page.exportToCSV;    
        Test.setCurrentPage(pageRef);
        system.currentPageReference().getParameters().put('currentRecordId', opp.Id);
        system.currentPageReference().getParameters().put('Referer', 'https://cs17.salesforce.com/006g000000BQRDf');

        exportCSV conObj = new exportCSV();
        Test.startTest();
        // Call Methods of the class
        Test.stopTest();
        // Write assert statements
        List<Account> insertedAccounts = [SELECT Name
                                      FROM Account
                                      WHERE Id =: opp.Id];
        
        List<Opportunity> insertedOpps = [SELECT Name
                                      FROM Opportunity
                                      WHERE Location__c =: 'Cork'];
                                     
        
                                      
    }   
}