+ Start a Discussion
whitfty1whitfty1 

Simple Trigger With Code Not Covered. Not Sure Why?

I've been trying to figure this out for about a week now and I know it has to be something simple that I'm missing.  What I'm trying to accomplish is create a trigger to fire after a case  that will see if case submitter has an associated partner that it should be assigned to.  I am doing this with two

queries and then the update.  Here is the trigger

 

trigger Reassign on Case (after insert) {
case c = trigger.new[0]; 

AccountPartner[] acctPrt = [Select AccountToId FROM AccountPartner WHERE AccountFromId IN (Select accountid FROM contact where Email = :c.SuppliedEmail)];   
  
Account acct = [Select isPartner FROM Account where ID=  :acctPrt[0].AccountToId]; 

if (acct.IsPartner == true) {    
c.OwnerID = '<some owner id>';
c.Status='Waiting Activation';
c.StatusReason__c='Assigned';
update c;
}
}

 

 Here is the test class and I plug in contact and account info from the sandbox

@isTest
public class TestReassign {
  static testMethod void myUnitTest(){
  	List<Case> cases = new List <case>{};
    Case s = new Case();
    s.SUPPORT_LEVEL__C = 'Gold';
     s.Status = 'Waiting Assignment';
     s.STATUSREASON__C = 'New';
    s.ENVIRONMENT__C = 'Development/Test';    
    s.VERSION__C = Decimal.valueOf(8);
    s.Origin = 'Phone';   
    s.Product__c = 'AB';
    s.AccountId = '<id>';
    s.SuppliedEmail = 'user@2.com';
    s.Subject = 'This is partner user ';
    cases.add(s);
    
        Case t = new Case();

     t.AccountId = ',id.';
    t.SUPPORT_LEVEL__C = 'Gold';
    t.Status = 'Waiting Assignment';
    t.STATUSREASON__C = 'Activated';
    t.ENVIRONMENT__C = 'Development/Test';    
    t.SuppliedEmail = 'user@1.com';
    t.VERSION__C = Decimal.valueOf(8);
    t.Origin = 'Web Portal';    
    t.Product__c = 'AB';
    s.ContactID = '<id>';   
    t.Subject = 'This is the ned user';
    
    cases.add(t);     
    
    
    test.startTest();
    try
    {
    	insert cases;
  
     
    }
    catch(System.DMLException e)
    {
     // System.assert(e.getMessage().contains('Record not added'));
    }
    
    test.stopTest();
}
}

 

What I receive is lines 19-23 not covered (beginning with "if (acct.IsPartner == true)"

 

Best Answer chosen by Admin (Salesforce Developers) 
SamuelDeRyckeSamuelDeRycke

Actually, there are a few things we need to adress.

 

First of all, your trigger should be bulkified, it may work right now, when you're testing for single objects, but when migrating data over dataloader or the API, your trigger will be called in bulk. Not a single object, but a list of objects will be passed to your trigger, which will not end well in your case, as you would only process the first element of each bulk send to your trigger.

An introduction to the concept  can be found here : http://wiki.developerforce.com/page/Best_Practice:_Bulkify_Your_Code  but there are many resources availble if you use a search engine.

 

Besides that, to minimize code dependency on data, its appropriate to insert all data your test method requirest at the start of it. Salesforce knows this is in test context, and the data will never be visible on your organisation, and will be cleaned after your test code has ran.  You're already making several Case objects, which is a good start !

Try making and inserting account records too. As you've got conditional logic based on the account.IsPartner field, you will need to run an insert on a case object at least twice, once with a parent account having IsPartner set to true, and once with IsPartner set to false to have full test coverage over that conditional branch of code.

 

On top of all that, you shouldn't just get your code covered, you should verify its behavoir is what you expect it to be ! using the System.Assert, AssertEquals and AssertNotEquals classes you can do this. This can seem a bit stupid, like "I just wrote all this code, of course it does what I want it to do", but sometimes you did leave a little bug, or someone may change it in the future, asserts will tell you when something isn't going the way you're expecting it to go. It can be a good practise to have someone else write the tests, independant of the implemented logic, or to write the tests in advance of writing the implementation. 
A good read on all this can be found here : http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

 

If you would give that a try, and post when you get stuck, we'll help where needed.

 

 

 

 

 

All Answers

SamuelDeRyckeSamuelDeRycke

Actually, there are a few things we need to adress.

 

First of all, your trigger should be bulkified, it may work right now, when you're testing for single objects, but when migrating data over dataloader or the API, your trigger will be called in bulk. Not a single object, but a list of objects will be passed to your trigger, which will not end well in your case, as you would only process the first element of each bulk send to your trigger.

An introduction to the concept  can be found here : http://wiki.developerforce.com/page/Best_Practice:_Bulkify_Your_Code  but there are many resources availble if you use a search engine.

 

Besides that, to minimize code dependency on data, its appropriate to insert all data your test method requirest at the start of it. Salesforce knows this is in test context, and the data will never be visible on your organisation, and will be cleaned after your test code has ran.  You're already making several Case objects, which is a good start !

Try making and inserting account records too. As you've got conditional logic based on the account.IsPartner field, you will need to run an insert on a case object at least twice, once with a parent account having IsPartner set to true, and once with IsPartner set to false to have full test coverage over that conditional branch of code.

 

On top of all that, you shouldn't just get your code covered, you should verify its behavoir is what you expect it to be ! using the System.Assert, AssertEquals and AssertNotEquals classes you can do this. This can seem a bit stupid, like "I just wrote all this code, of course it does what I want it to do", but sometimes you did leave a little bug, or someone may change it in the future, asserts will tell you when something isn't going the way you're expecting it to go. It can be a good practise to have someone else write the tests, independant of the implemented logic, or to write the tests in advance of writing the implementation. 
A good read on all this can be found here : http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

 

If you would give that a try, and post when you get stuck, we'll help where needed.

 

 

 

 

 

This was selected as the best answer
vishal@forcevishal@force

Hi,

 

In your test method, you haven't created any Accounts, so the query returns you 0 rows. 

 

You can make it work by two ways, either create Account and AccountPartner records in your test method. Or change your if condition to make sure it enters in whenever a test code is being executed:

 

if (acct.IsPartner == true || Test.isRunningTest()) {    
c.OwnerID = '<some owner id>';
c.Status='Waiting Activation';
c.StatusReason__c='Assigned';
update c;
}
whitfty1whitfty1

Thanks.  I kind of thought that but read that if you use existing accountswithin the sandbox that you wouldn't have to add accounts ..etc.  I will work on that now and thanks for the other tips, they are valuable.