Executing a static method from another class and adding parameters

apexcode-coverage

I need to fix some Apex test coverage and I have the the following issue:

I have my controller class like this:

public with sharing class someClass{

      public Boolean isTestClass = false

      @AuraEnabled
      public static String myMethod(String a, String b){
             if(isTestClass == true){
                return ('Covered Line')
             }else{
                return ('Covered Line')
             }
      }
}

And I want my Test class like this:

@isTest
public class myTestClass{

//SOME TEST SETUP BELOW @TestSetUp

      public static testmethod void myTest(){
             // here I want to do this
             someClass sc = new someClass();
             sc.isTestClass = true;
             sc.myMethod('a','b')
             sc.isTestClass = false;
             sc.myMethod('a','b')
      }
}

So it’s pretty much instantiating my controller class, set up a value and run two different tests one with false and one with true, so both if cases are covered but I have the following issues:

First, I can’t access the method from the instance because it’s an static method.

Second, the isTestClass variable is nonexistent inside myMethod().

How can I fix these two problems?

Btw, I can not change the class someClass that much and not even its methods. I was trying to send the isTestClass value over the myMethod parameter, but it affects other process that use that method and I can't do that.

Best Answer

Not recommending code like this but this will fix (at least in allowing compilation) one problem:

public static Boolean isTestClass = false;

and this the other:

public static testmethod void myTest(){
    someClass.isTestClass = true;
    someClass.myMethod('a','b')
    someClass.isTestClass = false;
    someClass.myMethod('a','b')
}

Do read up on this subject e.g. Static and Instance Methods, Variables, and Initialization Code.