Junit vs TestNG vs Mockito


JUnit and TestNG are the most popular testing frameworks for Java applications. Both frameworks are easy to use. So, when it comes to choose the testing framework for your application, it’s better to have a high−level idea of what features are present in one or the other and then take the informed decision based on your project requirements.

In this article, we will see JUnit vs TestNG.

Junit vs TestNG

JUnit current version is 5.7.1 and it’s still evolving and working on to include more features. TestNG current version is 7.4.0 and it’s mature and features rich.

Following table compares JUnit vs TestNG on different features. The table excludes very specific or common features that are present in both these frameworks, such as testing exceptions, timeout settings, lifecycle call−back methods etc.

Feature

JUnit

TestNG

Conclusion

Annotations

Annotations Based

Annotations Based

Both JUnit 5 and TestNG are annotation based. They are similar in nature and behaviour.

Ease of Use

JUnit 5 is built into various modules; you need JUnit Platform and JUnit Jupiter to write test cases. If you want more features such as Parameterized Tests, then you need to add junit−jupiter−params module.

Single module to get all TestNG feature.

TestNG is better in terms of ease of use.

IDE Support

Supported on major IDEs such as Eclipse and IntelliJ IDEA.

Supported on major IDEs such as Eclipse and IntelliJ IDEA.

Both of them are similar and provides easy integration with major IDEs.

Data Provider

Supports multiple ways to provide test data, such as methods, Enum, CSV, CSV Files etc.

Supports data provider methods and from test suite xml file

JUnit is better for injecting test methods input data

Test Suite

In JUnit, Test Suite is run using @RunWith and @Suite annotations.

TestNG uses an XML file to run Suite test.

HTML Reports

JUnit need external plugin maven−surefire−report−plugin to generate HTML reports.

TestNG automatically creates HTML reports for the test run.

TestNG HTML reports look outdated but it’s simply to use. If you have to share HTML reports with others, recommended to use JUnit.

Running from Java Main Method

We can use JUnit 5 launcher API to run tests from java main method.

We can use TestNG run() method to execute tests from the java main method.

Both of them supports execution of test cases from java main method.

Assertions

JUnit provides enough assertion methods to compare expected and actual test results.

TestNG provides enough assertion methods to compare expected and actual test results.

Both of them are similar in terms of Assertions support.

Assumptions

JUnit supports assumptions to skip tests based on certain conditions.

TestNG doesn’t support assumptions.

JUnit is better if you want to skip tests based on conditions.

Test Order

Junit does not support Test Order.

TestNG supports Test Order for ordering test methods according to priority attribute.

TestNG is better when you want to execute tests in specific order.

Disable Tests

JUnit supports many ways to disable and enable tests. For example, based on OS, JRE, system properties.

TestNG supports disabling tests but it’s limited in functionality.

JUnit is better when you want to disable or enable tests based on conditions.

Parallel Execution

JUnit 5 doesn’t support parallel execution yet.

TestNG supports parallel execution if run through XML suite.

TestNG is better for parallel execution as of now, JUnit 5 development is going on to support this feature.

Listeners

JUnit supports listeners through Launcher API, there is no mechanism to add listeners using annotations.

TestNG supports various types of listeners and can be added using annotations.

TestNG listener support is much better compared to JUnit 5.

What is Mockito?

Mockito is mocking framework, it is java based. The major difference between Mockito and TestNG/Junit is that Mockito is not a testing framework while later are. Mocking framework means generation of dummy data for a system that can be use for dry run or debugging. Mockito can be integrated with any testing framework such as Junit or TestNG or any other.

Mockito is a mocking framework, JAVA−based library that is used for effective unit testing of JAVA applications. Mockito is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in unit testing. Mockito facilitates creating mock objects seamlessly. It uses Java Reflection in order to create mock objects for a given interface. Mock objects are nothing but proxy for actual implementations.

Consider a case of Stock Service which returns the price details of a stock. During development, the actual stock service cannot be used to get real−time data. So we need a dummy implementation of the stock service. Mockito can do the same very easily, as its name suggests.

Updated on: 18-Aug-2023

327 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements