Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit 5 vs TestNG

Tags:

We were using JUnit 4, but we had some problems with test grouping. For integration tests we had problems with running order. We had migrated to TestNG in February 2016.

Now we are starting a new independent module for our project and part of our team players wants JUnit 5. I believe it is much better than 4th (there are some new important features, but I see some problems too).

I do not want to use JUnit 5 because of fear of possible problems in our new application, because I have experience with 4th. But I have to be more flexible for the team, so before making a decision I need recommendations from users who have experience with JUnit 5.

like image 810
Sergii Avatar asked Jul 08 '16 18:07

Sergii


People also ask

Is TestNG better than JUnit?

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.

Why TestNG is preferred over JUnit?

TestNG supports following three 3 additional setUp/tearDown level: @Before/AfterSuite, @Before/AfterTest and @Before/AfterGroup. TestNG do not require extend any class. TestNG allows to execute test cases based on group which isn't possible in JUnit. Parallel execution of Selenium test cases is possible in TestNG.

Can I use TestNG and JUnit together?

You can execute your existing JUnit test cases using TestNG. TestNG can automatically recognize and run JUnit tests, so that you can use TestNG as a runner for all your existing tests and write new tests using TestNG.

Is JUnit 4 better than JUnit 5?

Only one test runner can execute tests at a time in JUnit 4 (e.g. SpringJUnit4ClassRunner or Parameterized ). JUnit 5 allows multiple runners to work simultaneously. JUnit 4 never advanced beyond Java 7, missing out on a lot of features from Java 8. JUnit 5 makes good use of the Java 8 features.


2 Answers

JUnit 5 introduced a lot of new features. Currently Intellij IDEA supports JUnit 5.

Take a look at article about integrating: Using JUnit 5 in IntelliJ IDEA.

There are some useful annotations now, like:

  • @Tag Used to declare tags for filtering tests, either at the class or method level; analogous to test groups in TestNG or Categories in JUnit 4

  • @DisplayName Declares a custom display name for the test class or test method

See more: JUnit 5 User Guide

like image 98
Andrii Abramov Avatar answered Oct 07 '22 19:10

Andrii Abramov


TestNG objective is beyond unit test so it covers wider testing needs like Scenario Tests, Integration Test, Dependency Test, Ordering, Parallel Execution etc, but these feature are not supported in JUnit 5.

The JUnit 5 team have these above items in their road map, but they are extending a unit testing framework to support wider range of testing needs. Lets see how the new subsequent release of Junit 5 is able to address the wider testing need in comparison with TestNG

For Spring based application Junit fits best for doing the Unit and Integration Testing as it the default testing framework and Spring Provides lots of Test API with Mockito.

For general testing and Web Automation (Selenium) - TestNG is the de facto.

like image 21
Anver Sadhat Avatar answered Oct 07 '22 17:10

Anver Sadhat