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?
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).
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>
<!-- ... -->
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With