+ Start a Discussion
LAMCORPLAMCORP 

CODE COVERAGE 84% - WANTING 100% Please help :)

Hi all,

 

I have a written a trigger and Test class to go with but only getting 84%. I have searched high and low for some sort of reason why the 2 lines in red are not being covered but have had no luck and no idea. Can anyone help please?

 



trigger CopyDownME1ME2 on Line_del__c (before insert, before update) {

    List<String> CopyME1ME2 = new List<String>{};
    
    if (Trigger.isInsert) {
    for(Line_del__c a: trigger.new)
        a.suggested_ME1__c = a.ME1__c;
    
    for(Line_del__c b: trigger.new)
        b.suggested_ME2__c = b.ME2__c;
    }
    if (Trigger.isUpdate) {
    for(Line_del__c c: trigger.new)
    if (Trigger.new[0].ME1__c != Trigger.old[0].ME1__c)
        c.suggested_ME1__c = c.ME1__c;
    
    for(Line_del__c d: trigger.new)
    if (Trigger.new[0].ME2__c != Trigger.old[0].ME2__c)
        d.suggested_ME2__c = d.ME2__c;
    }
}

Code Coverage 84%

 
Best Answer chosen by Admin (Salesforce Developers) 
Andy BoettcherAndy Boettcher

You need to update your list with different values in the ME1 and ME2 fields to hit your conditionalities.

 

@isTest
Private Class CopyDownME1ME2{
Static testMethod void TestCopyDownME1ME2Trigger(){

        Line_del__c[] records = new Line_del__c[]{
                new Line_del__c (Name = 'Copydown2 Trigger Test',Team_s_Dept_s_del__c = 'a0P300000053psP', ME1__c = '00530000004WakH', ME2__c = '00530000004XFU6', DTR3__c = System.Today() )};
        insert records;
        
        List<Line_del__c> testDownME1Me2 = new List<Line_del__c>();
        for(Integer i=0; i<200; i++){
        testDownME1Me2.add( new Line_del__c (Name = 'Copydown3 Trigger Test',Team_s_Dept_s_del__c = 'a0P300000053psP', ME1__c = '00530000004WakH', ME2__c = '00530000004XFU6', DTR3__c = System.Today()));
        }
        insert testDownME1Me2; 
    
        test.startTest();
        update records;

/* START EDIT */
for(Line_del__c ld : testDownME1Me2) { ld.ME1__c = '00530000004XFU6' // Flipped from ME2 ld.ME2__c = '00530000004WakH' // Flipped from ME1 } upsert testDownME1Me2;

/* END EDIT */ test.stopTest(); } }

 

The standard disclaimer:  You shouldn't use hard-coded Ids in your Test class...always query for existing data or even better create your own data within your Test Class to ensure that it works across all environments and orgs.

 

-Andy

 

All Answers

Andy BoettcherAndy Boettcher

What's your Test Class look like?  Just looks like you need to change the values of the field and update your record.

LAMCORPLAMCORP

Hi,

 

Thanks for replying. Please see Test Class below.

 

@isTest
Private Class CopyDownME1ME2{
Static testMethod void TestCopyDownME1ME2Trigger(){

        Line_del__c[] records = new Line_del__c[]{
                new Line_del__c (Name = 'Copydown2 Trigger Test',Team_s_Dept_s_del__c = 'a0P300000053psP', ME1__c = '00530000004WakH', ME2__c = '00530000004XFU6', DTR3__c = System.Today() )};
        insert records;
        
        List<Line_del__c> testDownME1Me2 = new List<Line_del__c>();
        for(Integer i=0; i<200; i++){
        testDownME1Me2.add( new Line_del__c (Name = 'Copydown3 Trigger Test',Team_s_Dept_s_del__c = 'a0P300000053psP', ME1__c = '00530000004WakH', ME2__c = '00530000004XFU6', DTR3__c = System.Today()));
        }
        insert testDownME1Me2; 
    
        test.startTest();
        update records;
        test.stopTest();
    }
}
Andy BoettcherAndy Boettcher

You need to update your list with different values in the ME1 and ME2 fields to hit your conditionalities.

 

@isTest
Private Class CopyDownME1ME2{
Static testMethod void TestCopyDownME1ME2Trigger(){

        Line_del__c[] records = new Line_del__c[]{
                new Line_del__c (Name = 'Copydown2 Trigger Test',Team_s_Dept_s_del__c = 'a0P300000053psP', ME1__c = '00530000004WakH', ME2__c = '00530000004XFU6', DTR3__c = System.Today() )};
        insert records;
        
        List<Line_del__c> testDownME1Me2 = new List<Line_del__c>();
        for(Integer i=0; i<200; i++){
        testDownME1Me2.add( new Line_del__c (Name = 'Copydown3 Trigger Test',Team_s_Dept_s_del__c = 'a0P300000053psP', ME1__c = '00530000004WakH', ME2__c = '00530000004XFU6', DTR3__c = System.Today()));
        }
        insert testDownME1Me2; 
    
        test.startTest();
        update records;

/* START EDIT */
for(Line_del__c ld : testDownME1Me2) { ld.ME1__c = '00530000004XFU6' // Flipped from ME2 ld.ME2__c = '00530000004WakH' // Flipped from ME1 } upsert testDownME1Me2;

/* END EDIT */ test.stopTest(); } }

 

The standard disclaimer:  You shouldn't use hard-coded Ids in your Test class...always query for existing data or even better create your own data within your Test Class to ensure that it works across all environments and orgs.

 

-Andy

 

This was selected as the best answer
LAMCORPLAMCORP

Andy,

 

Thank you so much, you are the man..100%!!

I am really sorry I am a real novice and been trying to muddle my way through and make the best of Apex. I am aware that I can use 84% but I really want to learn how to code well. 

If you have time could you explain a couple of things please?:

 

1. You say "You need to update your list with different values in the ME1 and ME2 fields to hit your conditionalities."

Which parts of my code are you referring to when you say "conditionalities." I understand the list is to loop through 200 records. Are the conditionalities in the Trigger or in the Test Class?

 

2. When you say "You shouldn't use hard-coded Ids in your Test class...always query for existing data or even better create your own data within your Test Class to ensure that it works across all environments and orgs."

How do I do create my own? I did think about the fact if these ID's become obsolete one day.. What would happen. Is it something like below which I have seen a lot of?: (granted this is for the Account object)


[Select a.Id, a.Description From Account a Where a.Id = :Trigger.new[0].ConvertedAccountId];
        a.Description = Trigger.new[0].Name;
Andy BoettcherAndy Boettcher

You're very welcome!  My pleasure to help someone out.

 

1.  Conditionalities = "IF" statements.

 

2.  To follow "best practices", you should always create your own data in the test class.  In your Unit Test, in all of the places where you're referencing an Id, you'll want to create and insert records instead, then you can use those Ids.  The idea is to completely break your dependence on ANY of your org's data.

 

To illustrate....

 

This is relying on your org's data...


Account acct = [SELECT Id, Name FROM Account WHERE Something = 'A value' LIMIT 1];
..
...
Contact.AccountId = acct.Id;

 

 

This is what you should do...


Account acct = new Account();
acct.Name = 'Test Account';
insert acct;
...
...
Contact.AccountId = acct.Id;

 

 

Doing the latter method completely disconnects you from any data having to exist in your org.  You never have to worry about any data being deleted or Id going obsolete...

 

LAMCORPLAMCORP

Excellent thanks again. So helpful..

 

So if I was to rewrite my Test class to suit this best practice have I structured it correctly below and what would I fill instead of the ???

 

Line_del__c records = new Line_del__c();
records.Name = "Test Line";
records.Team_s_dept_s_del__c = ??? ;
records.ME1__c = ??? ;
records.ME2__c = ??? ;
records.DTR3__c = System.Today();
insert records;
insert records;
Andy BoettcherAndy Boettcher

You would create records in the objects behind those Ids.

LAMCORPLAMCORP

Ok thanks..

 

Don't fully understand but I will do some more research.. :smileyhappy: