+ Start a Discussion
Piersoft PPiersoft P 

Test.isRunningTest() In Apex class

Hi ,

I am unable write code cover for the below else condition because of Test.isRunningTest(), In every time if condition is executed, but else is not executing. help me on this. 

if ((Name__c != null && Cheque_Number__c != null && Branch__c != null ) || Test.isRunningTest()) {
errMsg = 'Name existing in database';
} else {
errMsg='';
}

Thanks in Advance

Regards,
raju
v varaprasadv varaprasad
Hi,

Create two test methods one for if and next for else. In the second method don't create any data.

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.


Thanks
Varaprasad
Salesforce Freelance Consultant/Developer/Administrator
@For Salesforce Project Support: varaprasad4sfdc@gmail.com


Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
 
Narayanan NatarajanNarayanan Natarajan

Hi Piersoft,

  Please rewrite your core code as

 if (Name__c != null && Cheque_Number__c != null && Branch__c != null ) {
     
         errMsg = 'Name existing in database';
  }
  if((Name__c == null || Cheque_Number__c == null || Branch__c != null) || Test.isRunningTest() {

      errMsg='';
}

NOTE : It will cover all your test cases and easy to cover those lines.

Thanks,

Narayanan Natarajan

Bhargavi TunuguntlaBhargavi Tunuguntla
Hi

Test.isRunningTest() is to execute the logic always when run through test Class.So your code only runs the if block .

Try removing the Test.isRunningTest() in if condition.
Write two methods in your test class, one method which satisfies the if block and other which doesn't .So both the blocks will be covered.

Thanks
Hope thsi will be useful
Ajeeth Kumar SAjeeth Kumar S

Hi there,

Test.isRunningTest() is to execute the logic always when run through test Class.So your code only runs the if block .
Please change you code like below or create a two method in test class to cover the class


 if ((Name__c != null && Cheque_Number__c != null && Branch__c != null) || Test.isRunningTest() ) {
     
         errMsg = 'Name existing in database';
  }
  if((Name__c == null || Cheque_Number__c == null || Branch__c == null) || Test.isRunningTest() {

      errMsg='';
}

 

Or 

 

Create one temp variable Like Below.

Integer temp=0;
 if ((Name__c != null && Cheque_Number__c != null && Branch__c != null) || Test.isRunningTest() ) {
     temp=1;     
         errMsg = 'Name existing in database';

  }
  if(temp==0 || Test.isRunningTest() {
      errMsg='';
}

Thanks!..

 

If it's helpful for you , Please marked my sollution as a answer.

Ajay K DubediAjay K Dubedi
Hi Piersoft,

When Test.isRunningTest() is running then 'else' part never be executed so don't write this type of condition otherwise
you need to change his code for code coverage and it is not good.
By two ways you can cover code:
1.
if ((Name__c != null && Cheque_Number__c != null && Branch__c != null )) {
errMsg = 'Name existing in database';

else {
errMsg='';
}
2.
if ((Name__c != null && Cheque_Number__c != null && Branch__c != null) || Test.isRunningTest()) {
errMsg = 'Name existing in database';

if ((Name__c == null && Cheque_Number__c == null && Branch__c == null) || (!Test.isRunningTest())){
errMsg='';
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi