You need to sign in to do that
Don't have an account?
testing of extension class methods failing
I have an extension class that i cant seem to test fully. It all stops on one method getContactId().
Here is my Extention Class:
public with sharing class FieldAccounts {
/*get the current case id*/
Private Final Case cse;
public FieldAccounts(ApexPages.StandardController stdController){
this.cse = (Case)stdController.getRecord();
}
public string getCaseId(){
return cse.id;
}
public boolean popWindow;
public Boolean tiedToAccount;
public boolean chooseAccount;
/*get the contact Info*/
public String contactId;
public String cseNumber;
public string getContactInfo(String cid){
Case[] cseconid = [select id, contactid, CaseNumber from Case where id = :cid];
contactId = cseconid[0].ContactId;
cseNumber = cseconid[0].CaseNumber;
return null;
}
/*get the contact ID*/
public string getContactId(){
getContactInfo(getCaseId());
return contactId;
}
/*get the case number*/
public string getCseNumber(){
getContactInfo(getCaseId());
return cseNumber;
}
/*see if the contact belongs to a vip account*/
public string faId;
public string getFaId(){
String conId = getContactId();
if(conId != null){
Contact[] con = [select id, logmein__Field_Account__r.Id from Contact where id = :conId limit 1];
faId = con[0].logmein__Field_Account__r.id;
if(faId != null && faId.length() > 1){
tiedToAccount = true;
popWindow = true;
}else{
tiedToAccount = false;
getContactEmail();
}return faId;
}else{return null;}
}
/*see if it is tied to a field account*/
public boolean getTiedToAccount(){
getFaId();
return tiedToAccount;
}
/*get the email address of the contact*/
public string contactEmail;
public string getContactEmail(){
String conId = getContactId();
if(conId != null){
Contact[] vCon = [select id, email from Contact where id = :conId limit 1];
if (vCon.size() != 0 && vCon != null){
contactEmail = vCon[0].email;
}
return contactEmail;
}else{return null;}
}
/*extract the domain from the contacts email*/
public String emailDomain;
public string getEmailDomain(){
string conE = getContactEmail();
if(conE != null){
contactUtils cUtils = new contactUtils();
String emailDomain = cUtils.getDomainFromEmail(conE);
//emailDomain = '%' + emailDomain + '%';
return emailDomain;
}else{return null;}
}
/*see if there are any field accounts that match that domain*/
public string accIds;
public string getAccIds(){
string eDom = getEmailDomain();
if(eDom != null){
FieldAccount__c[] fieldacc = database.query('select id from FieldAccount__c where domain__c like ' + '\'%' + eDom + '%\'');
if(fieldacc.size() > 0 ){
popWindow = true;
}
return accIds;
}else{popWindow = false; return null;}
}
/*see if the alert will fire*/
public boolean getPopWindow(){
getAccIds();
return popWindow;
}
public String getFAPageURL(){
PageReference pageRef= new PageReference('/apex/FieldAccounts');
return pageRef.getUrl();
}
public String getFACPageURL(){
PageReference pageRef= new PageReference('/apex/FieldAccountChoose');
return pageRef.getUrl();
}
}
My exten test class so far(since it is returning null on getting the contact id)
public with sharing class FieldAccountsTest {
/*create a fa account*/
public FieldAccount__c[] createNewFAccount(string faname, string dom){
FieldAccount__c[] ac = new FieldAccount__c[0];
ac.add(new FieldAccount__c(name = faname, domain__c = dom));
insert ac;
return ac;
}
/*get the id*/
public String faccountid;
public String getFAccountid(string faname){
FieldAccount__c[] getid = [select id from FieldAccount__c where name = :faname limit 1];
faccountid = getid[0].id;
return faccountid;
}
/*create a account*/
public Account[] createNewAccount(string aname){
Account[] ac = new Account[0];
ac.add(new Account(name = aname));
insert ac;
return ac;
}
/*get the id*/
public String accountid;
public String getAccountid(string aname){
Account[] getid = [select id from Account where name = :aname limit 1];
accountid = getid[0].id;
return accountid;
}
/*add contacts to the account*/
public Contact[] createNewContact(String name, String lastname, String email, String acId){
Contact[] nc1 = new Contact[0];
nc1.add(new Contact(firstname = name, lastname = lastname, email = email, AccountId = acId));
insert nc1;
return nc1;
}
/*get the contact id*/
public string contactId;
public String getContactId(String cemail){
Contact[] cid = [select id from Contact where email = :cemail];
contactId = cid[0].id;
return contactId;
}
/*create a case under the first contact*/
public Case[] createNewCase(String email, String name, String product, String subject, String description, String origin){
Case[] cse = new Case[0];
cse.add(new Case(contactemail__c = email, SuppliedName = name, Product__c = product, Subject = subject, Description = description, Origin = origin));
insert cse;
return cse;
}
public string cseid;
public string getCaseId(string cemail){
case[] cid = [select id from case where contactemail__c =:cemail];
cseid = cid[0].id;
return cseid;
}
public string csenum;
public string getCsenum(string cemail){
case[] cid = [select id, CaseNumber from case where contactemail__c =:cemail];
csenum = cid[0].CaseNumber;
return csenum;
}
/*Get user info for testing*/
public string curUserId = UserInfo.getUserId();
public string getCurUserId(){
return curUserId;
}
public string curUserName;
public string curUserEmail;
public string getUserInfo(){
user[] info = [select id, name, email from User where id =:curUserId];
curUserName = info[0].name;
curUserEmail = info[0].email;
return null;
}
public string getCurUserName(){
getUserInfo();
return curUserName;
}
public string getCurUserEmail(){
getUserInfo();
return curUserEmail;
}
static testmethod void testGetContactInfo(){
FieldAccountsTest t = new FieldAccountsTest();
t.createNewFAccount('ge', '@ge.com');
string facid = t.getFAccountid('ge');
t.createNewAccount('accountone');
string acid = t.getAccountid('accountone');
t.createNewContact('Joe', 'shmo', 'js@ge.com', acid);
String cId = t.getContactId('js@ge.com');
Case cse = new Case(contactemail__c = 'js@ge.com',SuppliedName = 'joe shmo',Product__c = 'Logmein Rescue',Subject = 'help',Description = 'need some help', Origin ='email');
insert cse;
ApexPages.StandardController sc = new ApexPages.StandardController(cse);
string cseId = t.getCaseId('js@ge.com');
string cseNum = t.getCsenum('js@ge.com');
FieldAccounts f = new FieldAccounts(sc);
system.assertEquals(cseId, f.getCaseId());
f.getContactInfo(cseId);
system.assertEquals(cId, f.getContactId());
system.assertEquals(cseNum, f.getCseNumber());
f.getFaId();
system.assertEquals(false, f.getTiedToAccount());
f.getContactEmail();
system.assertEquals('js@ge.com', f.getContactEmail());
system.assertEquals('@ge.com', f.getEmailDomain());
system.assertEquals('/apex/logmein__FieldAccountChoose', f.getFACPageURL());
system.assertEquals('/apex/logmein__FieldAccounts', f.getFAPageURL());
}
}
in the part
system.assertEquals(cId, f.getContactId());
f.getContactId() returns null. When i dig deeper, i found that even though 'system.assertEquals(cseId, f.getCaseId());' works and returns the case Id, in the getContactId() method it is null (only for the test class).
This is my first class extension that i have had to test, and all of my normal test classes that have very simular metods, if not the same, work. WTF???
Please i need some help!!
Did you forget to include your trigger that sets the contactId on the case by matching the custom email field value for case inserts?
Unless I misinterpreted your test code it appears that you are expecting the contactId to be set by this operation somehow which, by itself, it won't:
If there is no missing trigger, then update your test to set the contactID like this:
All Answers
Did you forget to include your trigger that sets the contactId on the case by matching the custom email field value for case inserts?
Unless I misinterpreted your test code it appears that you are expecting the contactId to be set by this operation somehow which, by itself, it won't:
If there is no missing trigger, then update your test to set the contactID like this: