JUnit 4 and TestNG used to be comparable. What are the pros and cons of the two testing frameworks?
Both Testng and Junit are Testing framework used for Unit Testing. TestNG is similar to JUnit. Few more functionalities are added to it that makes TestNG more powerful than JUnit.
Parameterized Test This feature is implemented in both JUnit 4 and TestNG. However both are using very different method to implement it.
JUnit is an open-source framework used to trigger and write tests. TestNG is a Java-based framework that is an upgraded option for running tests. JUnit does not support to run parallel tests. TestNG can run parallel tests.
I was comparing TestNG and JUnit4 today, and the main advantage that I can point out with my limited experience in testing-frameworks is that TestNG has a more elegant way of handling parametrized tests with the data-provider concept.
As far as I can tell with JUnit4 you have to create a separate test class for each set of parameters you want to test with (ran with @RunWith(Parameterized.class)
). With TestNG you can have multiple data-providers in a single test class, so you can keep all your tests for a single class in a single test-class as well.
So far that is the only thing that I can point out as a benefit of TestNG over JUnit4.
Intellij IDEA includes support for TestNG and JUnit out of the box. However, Eclipse only supports JUnit out of the box and needs a TestNG plugin installed to make it work.
However, a more annoying problem I ran into with TestNG is that your test classes need to extend PowerMockTestCase
if you are using PowerMock to mock dependencies in your tests. Apparently there are ways to configure the object-factory your test framework needs to know about when using PowerMock via a special method, or via the testng.xml
suite definition, but those seem to be broken at the moment. I dislike having test-classes extend test-framework classes, it seems hackish.
If you don't use PowerMock this is not an issue of course, but all in all I get the impression that JUnit4 is better supported.
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