How to cover if condition in test class in salesforce

image

It is very simple, you need to create two test method in your test class. In first method add conditions so that your if condition will be satisfied. In second method add conditions so that if condition will not be satisfied and else part will be executed. So you will have code coverage for if part as well as else part.

Full
Answer

What are the best practices for unit testing in Salesforce?

One of the other things that Salesforce recommends for unit testing is to put your test setup into a single method using the @testSetup annotation.

How to test if condition is satisfied in a test class?

It is very simple, you need to create two test method in your test class. In first method add conditions so that your if condition will be satisfied. In second method add conditions so that if condition will not be satisfied and else part will be executed. So you will have code coverage for if part as well as else part.

Does if (condition) block cover else condition in Test class?

ELSE condition in test class I tried this test class, but it only covers else part. it does not cover if (condition) block. Help me to achieve 100% code coverage. Thank-You! Please check once above code also. Thank you for the quick reply! but both the code is not working, I mean still IF block is not cover. Thank-You!

Is it feasible to attain branch coverage in Salesforce?

It’s not always feasible to attain branch coverage, but the code that you’ve shared is simple enough that it should be easy to accomplish. One of the other things that Salesforce recommends for unit testing is to put your test setup into a single method using the @testSetup annotation.

image

Leave a Comment