I have a trigger for an Opportunity:
trigger SolutionEngineer on Opportunity (after insert, after update) {
List<Trial__c> trialList = [select id, Opportunity_Name__c, Solutions_Engineer__c from Trial__c];
for (Opportunity opp :trigger.new) {
for (Trial__c trial : trialList) {
if(opp.id == trial.Opportunity_Name__c && opp.CloseDate > System.Today().addMonths(-6) && opp.IsClosed != True && opp.Sales_Engineer__c != trial.Solutions_Engineer__c ) {
trial.Solutions_Engineer__c = opp.Sales_Engineer__c;
update trialList;
}
}
}
}
I have a test for this trigger:
public class testSolutionEngineer {
static testMethod void TestSolutionEngineer(){
User u1 = new User();
User u2 = new User();
Account testAcc = new Account (name='Test Account Name', billingcountry='United States', billingstate='MD');
insert testAcc;
Contact testCont= new Contact (lastname='lastname', Leadsource='Event', ContactStatus__c='Active', Role__c='--Unknown--', Account=testAcc);
insert testCont;
Opportunity testOpp = new Opportunity (name='Test Opportunity Name', stagename='Lead(1)', CloseDate=system.today(), Account=testAcc, Sales_Engineer__c=u1.Id);
insert testOpp;
Trial__c testTri = new Trial__c(Trial_Type__c='Hosted');
testTri.Opportunity_Name__c=testOpp.Id;
testTri.Trial_Start_Date__c=System.today();
testTri.Trial_End_Date__c=System.today()+ 1;
testTri.Solutions_Engineer__c=u2.Id;
insert testTri;
update testOpp;
}
}
When I run my test, I am only getting 66% code coverage for my trigger:
I did some tests and specifically, opp.Sales_Engineer__c != trial.Solutions_Engineer__c
is evaluating as False
. What is going on?
Attribution to: Di Zou
Possible Suggestion/Solution #1
Your users are not inserted, therefore u1.Id == u2.Id == null.
Attribution to: Jeremy Nottingham
Possible Suggestion/Solution #2
Try this :
Both of them work and gives a 100% coverage on the trigger
Approach 1: User object can be queried and when you try to create user and try to Insert 2 users you will end up having mixed_dml_operations Since you want a real test case where you need to verify 2 users you can pick 2 random users
public class testSolutionEngineer {
static testMethod void TestSolutionEngineer(){
user u1 = [select id from user where profile.name = 'System Administrator' limit 1]; user u2 = [select id from user where profile.name = 'Standard Platform User' limit 1];
Account testAcc = new Account (name='Test Account Name', billingcountry='United States', billingstate='MD');
insert testAcc;
Contact testCont= new Contact (lastname='lastname', Leadsource='Event', Account=testAcc);
insert testCont;
Opportunity testOpp = new Opportunity (name='Test Opportunity Name', stagename='Lead(1)', CloseDate=system.today(), Account=testAcc, Sales_Engineer__c=u1.Id);
insert testOpp;
custom_object__c testTri = new custom_object__c ();
testTri.Opportunity__c=testOpp.Id;
//testTri.Trial_Start_Date__c=System.today();
//testTri.Trial_End_Date__c=System.today()+ 1;
testTri.Solutions_Engineer_look__c = u2.Id;
insert testTri;
update testOpp;
}
}
Approach 2: System.runas() The user info is submitted based on the running user chosen
public class testSolutionEngineer {
static testMethod void TestSolutionEngineer(){
profile p2 = [select id from profile where name = 'Standard Platform User'];
UserRole r2 = [Select id from userrole where name='CEO'];
User u2 = new User(alias = 'standu', email='standuser@testorg.com',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US',
localesidkey='en_US', profileid = p2.Id, userroleid = r2.Id,
timezonesidkey='America/Los_Angeles',
username='standuser@testorg.com');
// user u1 = [select id from user where profile.name = 'System Administrator' limit 1];
// user u2 = [select id from user where profile.name = 'Standard Platform User' limit 1];
system.runas(u2){
Account testAcc = new Account (name='Test Account Name', billingcountry='United States', billingstate='MD');
insert testAcc;
Contact testCont= new Contact (lastname='lastname', Leadsource='Event', Account=testAcc);
insert testCont;
Opportunity testOpp = new Opportunity (name='Test Opportunity Name', stagename='Lead(1)', CloseDate=system.today(), Account=testAcc, Sales_Engineer__c=u2.Id);
insert testOpp;
custom_object__c testTri = new custom_object__c ();
testTri.Opportunity__c=testOpp.Id;
//testTri.Trial_Start_Date__c=System.today();
//testTri.Trial_End_Date__c=System.today()+ 1;
testTri.Solutions_Engineer_look__c = null;
insert testTri;
update testOpp;
}
}
}
Attribution to: Rao
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4422