Mix TestNG and Junit Assertions Together within the Same Test?


JUnit and TestNG are the most popular testing frameworks for Java applications. Both frameworks are easy to use. But, they are different with each other. Their import libraries are different so the identifying the code is also different.

In this article, we will discuss whether it is possible to mix TestNG and Junit assertions together in same Test.

Let’s analyse how these two frameworks work.

TestNG

When user writes any @Test or other annotations, it needs to import a library as import org.testng.annotations.Test;

Here, testng is the key to identify the @Test or any other code execution is based on TestNG lib.

Similarly, when user writes an assertion, it is required to import the assertion library as import org.testng.Assert;

JUnit

When user writes any @Test or other annotations, it needs to import a library as import org.junit.Test;;

Here, junit is the key to identify the @Test or any other code execution is based on junit lib.

Similarly, when user writes an assertion, it is required to import the assertion library as import org.junit.Assert;

Mixing both

The priority goes how the @Test is defined whether as TestNG or Junit. If the @Test is defined as TestNG but Assertions are defined as junit.Assert, compiler won’t throw any exception but at run time user will get the error as

package org.testng.annotations does not exist.

And similarly, if Junit test is defined but assertions are imported as testng, it won’t be able to identify the assertion package.

Conclusion

The answer is NO, user can’t mix JUnit and TestNG assertions in same test or same class. However, they can be written into separate classes and run separately based on need.

Updated on: 18-Aug-2023

61 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements