Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does testng depend on junit?

testng brings junit to project as transitive Maven dependency. Isn't it supposed to be a replacement for junit?

from mvn dependency:tree:

[INFO] +- org.testng:testng:jar:6.9.6:test
[INFO] |  \- junit:junit:jar:4.10:test
[INFO] |     \- org.hamcrest:hamcrest-core:jar:1.1:test

Seriously, why? This causes constant pains because IDE offers to import both @Test annotations, they are almost identical in use, leading to having both junit and testng tests in project.

Is it safe to exclude junit transitive dependency? What might break?

like image 494
alamar Avatar asked Mar 14 '16 15:03

alamar


3 Answers

Disclaimer: I'm a TestNG contributor.

We replaced Maven by Gradle some releases ago, and we didn't find a way to define optional dependencies. Now it should be fixed since: https://github.com/cbeust/testng/pull/844

That explains why, with some releases, you can see JUnit as direct dependency of TestNG. But it is not the expected behavior and you can exclude it from the classpath without problem (if you don't need it to run some JUnit tests).

like image 93
juherr Avatar answered Nov 01 '22 08:11

juherr


Edwin Buck's answer is correct, but I want to add some information about the dependency itself. JUnit is marked as an optional dependency (see the pom):

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
    <optional>true</optional>
</dependency>

According to the Maven documentation, this means that you should be able to exclude the dependency if you do not need the feature:

The idea is that some of the dependencies are only used for certain features in the project, and will not be needed if that feature isn't used.

So, at least the dependency is designed to be able to be excluded.

To elaborate a bit more on how TestNG and JUnit are connected, features for which you would likely need the dependency, TestNG has support for direct execution of Junit tests. According to the documentation, this would be implemented something like:

<test name="Test1" junit="true">
    <classes>
        <!-- ... -->
like image 27
Magnilex Avatar answered Nov 01 '22 08:11

Magnilex


Because testng provides JUnit integration, and therefore can run JUnit tests within a testng framework. Basically it was added as a path to migrate JUnit tests from the "JUnit way" to the "testng way". You can read more about it here.

I don't know if testng will operate with this integration removed; but, you can always remove the transitive dependency and find out.

like image 3
Edwin Buck Avatar answered Nov 01 '22 09:11

Edwin Buck