JUnit Test Proposal

All,

When writing a test method for method A (which has many internal conditions), should I focus on testing one condition every time? I find it difficult to structure a testing method for method A, which will cover the entire code path in method A.

Can anyone suggest me how to start writing a test method?

+3
source share
5 answers

My preference is to have one point of failure in the JUnit testing method. This means that I will have many JUnit testing methods for the target class method that I am testing.

testA1(), testA2(), testA3() (A). .

A 8 , 8 , , , .

+1

. , , . , , .

unit test . , . , .

+3

. ,

if (cond1 || cond2) {....}

cond1 cond2. , .

testMyMethodCond1(){...}  

testMyMethodCond2(){...}

, . , , " ", , , , , .

+2

, , . -, . . () .

+1

Tests should be simpler, preferably much simpler than the tested thing. Otherwise, the error will most likely be in your test. Therefore, it is much better to have many small simple testing methods that perform small parts of your method than one big clumsy one. (You can use a code coverage tool such as Cobertura to make sure that you cover all the paths in your method.)

+1
source

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


All Articles