TDD: why can it be wrong if the application code knows that it is being tested and not launched?

In this thread, Brian (the only responder) says: "Your code should be written in such a way that it tests agnostic."

One comment says: "Your code should definitely not be in the global" I'm checking the flag. "

But none of the reasons gives reasons, and I would really like to hear some rational thoughts on this issue. It would be very simple (especially considering the fact that many tests have access to private access to application classes) to encompass this application class and set the logical expression “this is a test, not a run”.

All kinds of things that I find myself jumping through hoops (injected bullying of private fields, etc.) to achieve, can become easier.

It is also obvious that if you have done this too much, it can be disastrous ... but as one of the many tools in the arsenal of software testing, why does the concept come up with such an opprobrium?

Reply to Mick Mnemonic:

A trivial example of how this might help is to create a new instance of the class in the middle of the method and assign it to the private field: private mocks in this case will not help, because you are replacing the private field. But actually creating a real object can be very expensive: you might want to replace it with a light version when testing.

, ... , - createXXX()... . , , " , "!

+4
5

. , , .

, , . .

1:

, -.

. .

:

  • .
  • .
  • .
  • - - .

. . , / .

2: ,

, .

, . , , .

, . , .

?

, :

  • .

    ,... , ? , , , quicksort.

  • .

    , , . , .

    , , . , . , .

, ?

/ . , , .

-

, if (TEST_MODE) , .

:

public class Startup {

    private static final boolean TEST_MODE = false;

    public static void main(String[] args) {
        if (TEST_MODE) {
            TestSuite testSuite = new TestSuite();
            testSuite.execute();
        } else {
            Main main = new Main();
            main.execute();
        }
    }
}

, , . if (TEST_MODE) , , .

, Java - JUnit TestNG, if (TEST_MODE).

, .

  • , , .

    mocks , , . , , . , , .

    .


, , ++ Test-Driven Development . , , TDD, - .

++ , TDD. , () . :

++, ? , . ++ , , Java, #, C Ruby .

+2

Volkswagen. , - , , . : , - , , - . , , . , , - .

+3

, , . , / . TDD, / , .

: " " ".

- , . , . , / , .

+2

TDD: , , , ?

1) . , , , , .

2) Test-Driven Development , , , . , .

TDD, , TDD , , , , unit test , , .

, , , , , .

3) Test-Driven Development - , , .

, , , : . : .

, ... - createXXX()... . , , " , "!

package-private , , .

" , " .

: " , , API "

, , , TDD, , . < > , , , , .., , .
TDD.

, , API , . , , API .

, MyClass :

@Service
public class MyClass{
...
MyDependency myDependency;
...
@Autowired
public MyClass(MyDependency myDependency){
   this.myDependency = myDependency;
}
 ...
}

, API MyClass , . , MyClass , myDependency:

@Service
public class MyClass{
...
MyDependency myDependency;
...
@Autowired
public void setMyDependency(MyDependency myDependency){
   this.myDependency = myDependency;
}
 ...
}

: 4 5 , . , -, , , , , , . , .

0

, . , , : , , TDD, . , , , , 6 .

Carl Manaster Volkswagen , , : , "", , .

, , , , , , "" TDD.

:

, , , , . : doThrow( ... ) @Test( expected = ... ), . , . . , logback-test.xml . , -, .

, , , :

boolean suppressStacktrace(){ return false; };

... LOGGER.error( ..., true, .

-, : BufferedReader.readLine(). InputStream System.in List Strings, readLine, . , private :

Deque<String> inputLinesDeque;

... a package-private, List<String> , pop ped, Deque . Deque null, if br.readline().

2 . , , - , , .

, davidxxx TDD 10 : " , , API ". : .

, logback... , logback, logback-test.xml " ". , , logback , ... , " ". "" ?

0

Source: https://habr.com/ru/post/1667810/


All Articles