You need to sign in to do that
Don't have an account?
LAMCORP
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;
}
}
You need to update your list with different values in the ME1 and ME2 fields to hit your conditionalities.
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
What's your Test Class look like? Just looks like you need to change the values of the field and update your record.
Hi,
Thanks for replying. Please see Test Class below.
You need to update your list with different values in the ME1 and ME2 fields to hit your conditionalities.
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
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)
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...
This is what you should do...
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...
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 ???
You would create records in the objects behind those Ids.
Ok thanks..
Don't fully understand but I will do some more research.. :smileyhappy: