1

I am stuck with this if clause in my Test Class. Can anybody help me? Here is my code example.

public MethodName(ApexPages.StandardController Controller){

    Newflag = false; //(variable Initialized as false)

    if(Newflag)
    {
        // Do some stuff
    }
    //No else present or needed in this case.
}

When I am trying to pass this method by calling MethodName in my test class, and initializing the Newflag as true in the testclass, it not getting passed, as NewFlag is already initialized as false in the main class, and the testclass flag is getting overwritten by false.

What should it do?

6
  • 1
    I do not see any way to execute the code in that if as it will always be false. It does not looks like a valid scenario. What is the use of making it false. That is a deadlock.
    – Ashwani
    Feb 3, 2017 at 10:11
  • This is a not a deadlock per definition, it's an useless if as it'll never go through the test and end up in the else every time :-) Feb 3, 2017 at 10:13
  • 3
    @Ashwani that is not a deadlock. A deadlock happens when 2 threads are blocked waiting for a resource that's block by the other. It's just unreachable code that will never run.
    – mkorman
    Feb 3, 2017 at 10:23
  • 1
    @mkorman Thats the british english word, I didn't follow the technical explanation. The code inside if is secure by the boolean which can not be changed.
    – Ashwani
    Feb 3, 2017 at 10:40
  • 1
    @ashwani fair Enough. I'm a British English speaker too. In programming terms, a deadlock is a very specific situation that can happen in concurrent programming: en.wikipedia.org/wiki/Deadlock . I was mistaken by the context and thought you were using the programming term :)
    – mkorman
    Feb 3, 2017 at 10:50

2 Answers 2

6

This is a classic example of where a test highlights an issue in your code. If your code really is the way you have written it, the statements inside the "if" will never execute.

In order for that code to execute, there must be a way to set that flag to true... possibly in a different method?

Another option is to pass the flag as an argument:

public MethodName(ApexPages.StandardController Controller, Boolean Newflag){

    if(Newflag)
    {
        // Do some stuff
    }
    //No else present or needed in this case.
}

That way you can supply the flag as an argument.

OR

Boolean NewFlag = false;

public SetFlag (Boolean flag) {
   Newflag = flag;
}

public MethodName(ApexPages.StandardController Controller){

    if(Newflag)
    {
        // Do some stuff
    }
    //No else present or needed in this case.
}

Either way will allow you to set the value of the flag externally and use it in your tests.

2
  • Hey guys, thanks a ton for ur useful inputs. Its a 4000+ line code with 7000+ lines of testclass. I am asked to increase Thr code coverage. I am new to this and drowning in codes and methods. I cant pist the code due to company security policies, but i am looking into it more closesly. Will get back to u helpful people if i get stuck again. Thanks a lot again
    – Abhishek
    Feb 3, 2017 at 12:49
  • 2
    @Abhishek No matter how you slice it, that is going to be a grueling task. Take heart, don't let it get you down, and don't write code like that. The key to writing testable code is Separation Of Concerns (more atomic methods). The key to writing good tests is not coverage, but rather verifying application behavior (rigorous assertions).
    – Adrian Larson
    Feb 3, 2017 at 15:47
0

That If block is useless code as it will never execute. you can simply discard from your code. if you still want to cover it in test class then update if condition to

if(Newflag || Test.isRunningTest()){
    // Do some stuff
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.