O'Reilly logo

Quality Code: Software Testing Principles, Practices, and Patterns by Stephen Vance

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Avoid Test Code in Production

Have you ever seen code like that in Listing 5-3?

Listing 5-3: Example of test code in production

public class MyClass {  private boolean testing = false;  public void doSomething() {    if (testing) {      // Mock something up    } else {      // Run the real code    }  }}

I’m sure you have. Unfortunately, something like this or one of its variations exists in most code bases. Let’s go over all the things that are wrong with this from a software-verification point of view.

First, how is testing set? I may have omitted a setter that would allow directly controlling the behavior through the interface. That would probably be the safest way to manage it. I’ve also seen people use a toggle ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required