You need to sign in to do that
Don't have an account?
Hazel 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
}
}
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
}
}
First in your test class you need to set the value for referrer.
And in your code put the null check
All Answers
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
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!
Sorry should have mentioned, line 9 in exportCSV is
line 13 in the test class is the initialisation
Soemthing like Since there was no page set, the 3 line in the above code was throwing you a null pointer exception
First in your test class you need to set the value for referrer.
And in your code put the null check
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.
What is this script doing on the page?
No need to specify cValid value for content type is just
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!
exportCSV Class:
Test Class: