1

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:

enter image description here

I did some tests and specifically, opp.Sales_Engineer__c != trial.Solutions_Engineer__c is evaluating as False. What is going on?

2 Answers 2

4

Your users are not inserted, therefore u1.Id == u2.Id == null.

3
  • 3
    I just want to point out that you're doing an update in the middle of a loop, which grates like nails on a chalkboard, and makes it hard to read the rest of your code. Commented Nov 14, 2012 at 22:03
  • I moved the update to outside the loops. I also inserted the Users. I still have the same issue.
    – Di Zou
    Commented Nov 14, 2012 at 22:29
  • Try putting in a system.debug statement to echo what the two user Ids are. Also, if you have updated code that gets the error, it would help to see the new code in your question. Commented Nov 15, 2012 at 0:05
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='[email protected]', 
            emailencodingkey='UTF-8', lastname='Testing', 
            languagelocalekey='en_US', 
            localesidkey='en_US', profileid = p2.Id, userroleid = r2.Id,
            timezonesidkey='America/Los_Angeles', 
            username='[email protected]');
// 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;
}
    } 

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .