You need to sign in to do that
Don't have an account?
Error during test does not see class variable EmailCounter
Error at run time: Variable does not exist: ProgramFlowExperiment.EmailCounter
----------------------------
Running this example of a test class using a trigger fails when I get to the TestContactOwnerChange class. The EmailCounter variable is not visible and the System.assertEquals does not see the variable EmailCounter created in the ProgramFlowExperiment class.
------------------------------------
public class ProgramFlowExperiment {
public static Integer EmailCounter = 0;
private void SendEmail(){
EmailCounter++;
System.Debug('Queueing the email to send');
}
private void SendQueuedEmails(){
System.Debug('Sending queued emails');
}
public void HandleContactUpdateTrigger(List<Contact> newlist, Map<Id, Contact> oldmap){
for(Contact ct: newlist){
if(ct.OwnerId != oldmap.get(ct.Id).OwnerId){
SendEmail();
}
}
SendQueuedEmails();
}
}
------
trigger ContactOwnerChangeTrigger on Contact (after update) {
ProgramFlowExpermient pf = new ProgramFlowExperiment();
pf.HandleContactUpdateTrigger(trigger.new, trigger.oldmap);
}
-------
@isTest
private class TestContactOwnerChange {
static testMethod void TestOwnerChange() {
// Create some tests
List<Contact> newcontacts = initTextContacts('c', 5);
user u = initTestUser('myname', 'myname');
System.runAs(u){
//These contacts will be created with the fake user as owner
insert newcontacts;
}
Test.StartTest();
for(Contact ct: newcontacts){
ct.OwnerID = UserInfo.getUserId();
ct.Email = 'someone@somehwere.com';
}
update newcontacts;
Test.stopTest();
System.AssertEquals(newcontacts.size(), ProgramFlowExperiment.EmailCounter);
}
public static List<Contact> initTestContacts(String prfix, Integer count){
List<Contact> results = new List<Contact>();
for(Integer x=1; x <count; x++){
results.add(new Contact(LastName = prefix + '_' + string.valueOf(x),
email = prefix + '_' + string.valueOf(x) + '@apexfundamentals.com' ));
}
return results;
}
public static User initTestUser(String username, String thealias){
User u = new User(Alias = thealias,
Email = username + '@apexfunadamentals.com',
FirstName = 'Joe', LastName = username,
TimeZoneSidKey = 'America/Los_Angeles',
UserName = username + '@apexfundamentals.com',
UserPermissionsMarketingUser=true, LocaleSidKey='en_US',
EmailEncoding='UTF-8', LanguageLocaleKey = 'en_US');
u.ProfileID = userinfo.getProfileId();
return u;
}
}
Hi Mitchellb,
You would need to change the declaration of the variable to define a public getter/setter: Try this and your variable should be visible:
You an either initialise the valiable in the get initialiser blok or in the constructor. If this helps mark this as a solution and add kudos....
I made the follow changes based on your suggestion and the exact same error occurred. The variable is not visible when selecting the test class -> Force.com -> Run Tests
public
class ProgramFlowExperiment {
publicstatic Integer EmailCounter {get; set;}
public ProgramFlowExperiment(){
this.EmailCounter = 0;
}
privatevoid SendEmail(){
EmailCounter++;
System.Debug(
'Queueing the email to send');
}
privatevoid SendQueuedEmails(){
System.Debug(
'Sending queued emails');
}
publicvoid HandleContactUpdateTrigger(List<Contact> newlist, Map<Id, Contact> oldmap){
for(Contact ct: newlist){
if(ct.OwnerId != oldmap.get(ct.Id).OwnerId){
SendEmail();
}
}
SendQueuedEmails();
}
}
Thank you for your response. I am new to Force.com. Maybe I am not compiling correctly to give visibility between the ProgramFlowExperiment class, the ContactOwnerChangeTrigger, and the TestContactOwnerChange class. The only step that I take to 'compile' is to right click on the TestContactOwnerChange class Force.com -> Run Tests. Am I missing a step?
Hi VBS, would you like me to understand this?