Is it good to use the JUnit test method in the test class?

Consider a Java class with small static library-style methods.

Is it wrong to use JUnit testing methods in one class?

I see the following pros:

  • JUnit test as stand-alone documentation is next to the method code
  • It will be easy to move this code to another class or package.

What are contrasts and why is it a common practice to always separate test code?

Example:

import org.junit.*;

public class HtmlUtils {
    public static String normalizeBrs(String html) {
        return html.replaceAll("<br\\s*/?>", "<br/>");
    }

    @Test
    public void testNormalizeBrs() {
        Assert.assertEquals(normalizeBrs("hello <br /> world <br>"), "hello <br/> world <br/>");
    }
}
+4
source share
6 answers

(Is it wrong to apply the JUnit test method to the test class?)

Absolutely .

: . , , / .

- " ".

: - - -. .

, ... - .

- "", , , .

, .

- x.y.Z... x.y.ZTest; , , .

: Java " " 2017 . , () . ( , , , : )

, , , , . , . API ; .

+3

, .

, , 1000 , 10 . 10 .

, + ?

- , , - .

. , .

+2

.

, .

  • . .
  • . , , . , , .
  • - , . .

.

!!!

+1

, :

  • (, , jar/war/ear, , ) (, Mockito,...),
  • , , , .
  • , api ( , ).
  • , . of maven
+1

- , Maven .

src/main src/test, .

src/main .

src/test .

.

. , jUnit . , (JMock, Mockito), , Hamcrest, , WireMock RestClientDriver, ..

, , .

, . , .

Maven , . , jUnit, Mockito .. , - , .

:

  • .
  • , ,

, Java-, , . .

:

  • " JUnit "

, . , , , , . , C #IFDEF TEST. , "" -, , , !

Maven Java-, , . IDE ( ) unit test - .

  • " "

, IDE ( ) . , , IntelliJ IDEA, , - , .

-

, , Maven, Gradle - .

0

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


All Articles