triangle
arrow
JUnit Vs TestNG

05-Oct-2020

By Ashish Sethi

JUnit Vs TestNG

Quick Comparison

Summary:

JUnit and TestNG are the very popular unit testing framework used for the Java programming language. Testers are very familiar with these terms because every day, they work on it and discuss many more queries in their community.

There are two or more programmer-friendly frameworks available on the internet for unit testing and for other types of programming languages. If you are an experienced software tester or have just started your career in software testing, I am sure that you know some basics, have some training or are already working on both of them at your office. No matter how much experience you have acquired in the software testing field, the important thing is you have to brush up your skills and should never stop learning even if you have a dream job. Junit and TestNG are essential for Java developers because these frameworks have the same test roots; you can call them siblings. I am invited to read our blog at the free of cost as it will cover the complete difference between two testing frameworks that are JUnit and TestNG and how they are different from each other and will let you know the best-suited framework for unit testing. I hope you will enjoy it.

Our Index:

  • What is JUnit?
  • Steps to Download and Install JUnit in Eclipse
  • How to Configure Environment Variables in Java: Path & Classpath
  • What is TestNG?
  • Comparison Chart of JUnit and TestNG Features.

1) What is JUnit?

JUnit is an open-source testing framework used to test Java applications. It plays a vital role in the development of test-driven development.

It is the family member of the unit testing framework known as XUnit, which originated with SUnit. The framework is used for writing and running tests.

2) Steps to Download and Install JUnit in Eclipse (Consider it for Practical Implementation)

You can install JUnit in just six parts. The detailed information is given below:

First Part: Install Java in Your System

As we discussed above, JUnit is a testing framework used to write and run Java-based applications. So, you have to configure or verify JDK (Java Development Kit) in your machine before installation. If you have Java JDK 8 in windows, you can directly continue with our second step. For those, who don’t know from where to download and how to install Java JDK in windows, please check the simple steps below:

Step 1: Click on the link.

Step 2: Now, you can see there is an option Download JDK, click on it

Step 3: In this step: Accept License Agreement and Download the latest Java JDK for your version (32 bit or 64 bit) of Java for Windows. 

Step 4: After completion of the download, run the exe for install JDK and click Next.

Step 5: Choose the PATH for Java installation and click Next.

Step 6: First, let the installation complete, and then click Close.

How to Configure Environment Variables in Java: Path & Classpath

The PATH variable provides the location of executables, such as java, Javac, and so on. You can run a program without specifying the PATH. It looks professional if you give a full path to the executable.

For example: C:\Program Files\Java\jdk-13.0.1\bin\javac A.java instead of simple javac A.java

CLASSPATH Variable: It gives the location of the library files.

Check-out the given steps to configure the PATH and CLASSPATH:

  1. Right-click on My Computer option and Choose the properties.

2. Click or Tap on advanced system settings.

3. Now, click on Environment Variables.

4. Click on the New icon/button of user variables.

5. Type PATH in the variable name.

6. Copy the Path of bin folder that is installed in your JDK folder.

 

7. Now, Paste Path of bin folder in Variable value. Click on OK button.

Point to Remember:

If you have a PATH variable in your system, you can directly edit the PATH variable to

PATH = \bin;%PATH%;

In this, %PATH% appends the existing path variable to our new value.

8. You can apply the same process to set CLASSPATH.

Point to Remember:

After installation, if java installation doesn’t work, you can change classpath to

CLASSPATH = \lib\tools.jar;

9. Click on ‘OK’ icon/button.

10. Open command prompt and type javac commands. If you can see the same screen as given below, congrats your Java is installed.

Second Part: Download JUnit in Your System

1. Go to the site and click Download and Install.

2. Click on junit.jar
 

3. As you can see, all versions of JUnit can be downloaded from the central repository. You can pick the latest version and Click on the jar link to download the version of JUnit. 

4. Go to https://github.com/junit-team/junit4/wiki/Download-and-Install and click hamcrest-core.jar

5. In this step, you need to Download the jar.

Similarly, you will require JUnit jars for JUnit installation. You can download the version of the JUnit jar file from its official website.

The jars list includes:

  • JUnit.jar
  • Hamcrest-core.jar

Third Part: JUnit Environment Setup

Step1: You require to adjust the JUNIT_HOME environment variable to confirm the base location in which you placed JUnit jars. For Instance: If you already created a JUnit folder in C drive of the computer and placed jars there, then you need to go into the Environment Setting and open control panel->advanced ->environment variable.

1. Click on the ‘New’ button under the environment window. It will open another window when you click on the new button in Environment Variables.

2. A “New System Variable” window opens:

  • Enter the variable name as "JUNIT_HOME".
  • Give JUnit value as a JUnit path where you have copied JUnit jar files and then click OK.

When you tap on OK, it will make a new system variable with the given name and value that you can confirm in the environment variable window, as shown in the first image.

3. After making JUNIT_HOME, you can create another variable with the name CLASSPATH. Now, go to Environment variables again and follow the next steps:

  • Click on the “New” button. It will open another window after the click on “new” in environment variables.

4. This step is pointing out JUNIT_HOME to JUnit.jar that is placed in the JUnit folder as mentioned below:

  • Variable Name: CLASSPATH
  • Variable Value: %CLASSPATH%;%JUNIT_HOME%\JUnit4.10.jar;.;
  • Tap on the OK button.

 

5. When you click on the ‘OK’ button, it will allow you to verify the new environment variable with the name “CLASSPATH” that you can see under the system variable. For more, check below:

 

Fourth Part: Install JUnit jar file in eclipse

1. Right-click on the project: Click on “build path” and then click on “Configure build path”.

2. In this step, go to java build path the same as the picture and click on the “Add External Jars” button to insert your downloaded JUnit.jar file with eclipse. After adding a JUnit.jar file, you can click to the ‘OK’ button to close the java build path window.

Fifth Part: Verify the required jar file for JUnit in your build path

To verify the JUnit jar file in eclipse, you can follow the given steps:

  • Right-click on the project -> Build Path.
  • Click on "Configure build path".

For Java build path: the window will appear as shown below:

Go to the Libraries tab to see all jar files in that window. In the jar file tree view, you can look for the jar file with the name started with JUnit. After extending JUnit libraries, you will see java libraries as specified in the picture.

Sixth Part: Confirm JUnit Setup

Create a basic JUnit test to confirm the JUnit setup. You can also check a given test class:

1. You can create a java class named TestJUnit.java and provide a basic assert statement.

2. Now build a Test Runner Class to implement the test as mentioned above.

3. Below are the steps to execute the test:

  • Right-click on TestRunner.java. Click on "Run As" as displayed below.
  • You will see another window while clicking on "Run As", click on "1 JUnit Test" as shown below:

4. The output of your test will appear the same as the given picture. If you have successfully executed the test, it will display a green check mark at the front.  You can save the images in your gallery folder for further testing practices.

What is TestNG?

TestNG is a testing framework released after JUnit for serving the same purpose, but with some extra functions.

TestNG is ideal for the Java programming language, created by Cedric Beust and powered by NUnit and JUnit frameworks. It covers the broad range of test categories, such as functional testing, end-to-end testing, unit testing, integration testing, etc. The term ‘NG’ in the TestNG refers to the “next generation.” It was first created in 2004, and the current version of TestNG is now 7.00 comes with robust and simple functionalities.

Comparison Chart of JUnit and TestNG Features.

The features of both JUnit and TestNG are almost the same, except one or two. Here is the simple comparison table that you can see to get the idea of the right technology for Unit Testing.

 

Junit

TestNG

Features

Yes

Yes

Annotation Support

Yes

Yes

Ignore Test

Yes

Yes

Suite Test

Yes

Yes

Exception Test

Yes

Yes

Parameterized Test

Yes

Yes

Timeout

No

Yes

Dependency Test

 

JUnit Versus TestNG Annotations in Testing

Both JUnit and TestNG frameworks support annotations in testing. Some features are very same, and Some of them don’t exist in one particular framework.

For instance,

@ignore annotation is used in JUnit to disregard a function similar to the @Test (enable = false) annotation in TestNG. Furthermore, @Before JUnit annotation is identical to the annotation @BeforeMethod in TestNG. In JUnit 4, you had to declare @AfterClass and @BeforeClass as a static method. However, no such constraints apply to TestNG.

Complete Annotations Difference Between JUnit and TestNG

 

JUnit 5

TestNG

Function

@Test

@Test

Test annotation

@BeforeAll

@BeforeClass

Perform before the first test method in the current class is invoked.

@AfterAll

@AfterClass

Execute after all the test methods in the current class have been run.

@BeforeEach

@BeforeMethod

Execute before each test method.

@AfterEach

@AfterMethod

Execute after each test method.

@ignore

@Test(enable=false)

Ignore Test

@Test(expected=Arithmetic Exception.Class

@Test(expectedException=Aiithmetic Exception.class)

Expected exception

@Test(timeout = 1000)

@Test(timeout = 1000)

Timeout

N/A

@BeforeSuite

Execute before all tests in this suite have run.

N/A

@AfterSuite

Execute after all tests in this suite have run.

N/A

@BeforeTest

Execute before the test.

N/A

@AfterTest

Execute after the test.

N/A

@BeforeGroups

Execute before the first test method that belongs to any of these groups is invoked.

N/A

@AfterGroups

Execute after the last test method that belongs to any of these groups is invoked.

 

Ignore Test

It is used to ignore a particular group of tests during execution. JUnit and TestNG include the ignore test feature in their frameworks, but you can see both are declared differently. In JUnit, you can use @ignore annotation to avoid the unit test. However, @Test(enabled = false) in TestNG allows us to ignore a test.

Code Snippet of JUnit (Example):

Code Snippet of TestNG (Example):

Suite Test

Test Suite or Suite Test is used to implement multiple tests together. You can create a Test Suite in JUnit4, JUnit5, and TestNG. Test suites in TestNG are very powerful and its methods are different to execute the test.

@RunWith and @Suite are annotations to use in JUnit and run a test suite:

TestNG allows to run test suites from the XML file :

Exception Test

If you need to check which exception is thrown from the test, You can perform exception testing in both JUnit4 and TestNG.

Use of exception test while working with JUnit4:

Use of exception test while working with TestNG:

Parameterized Test

JUnit gives a simple and effortless approach to execute Parameterized tests. Running a test case by supplying different values at each execution is known as Parameterized tests. These tests can reduce the code length and are great to use in data-driven testing.

How to do Parameterized Tests in JUnit4?

You can USE @RunWith and @Paremeter annotations in JUnit4 to get parameter value during unit testing. @Parameters will provide a return List[] that you can pass into the class constructor as an argument.

How to do Parameterized Tests in TestNG?

@DataProvider or XML file provides a parameter for testing with TestNG. You have to use @Parameters annotation for method declaration as the test needs a parameter to execute testing operations. The data used as a parameter will be given in XML configuration files of TestNG. You can even repeat the same test case for multiple data sets as it can produce several results.

 

XML File for Above Class:

Timeout

The feature is used to terminate a test in both JUnit4 and TestNG. Both JUnit4 and TestNG allow a test to end if it takes more than the defined period (in milliseconds).

Here is the code snippet to use the timeout test with JUnit4:

Here is the code snippet to use timeout test with TestNG:

Dependency Test

Currently, JUnit doesn’t support dependency tests, but TestNG does as it is the feature in which one test method is dependent on another method. The reason is JUnit only supports unit testing. The TestNG framework is beneficial for a wide variety of test types.

@dependsOnMethods annotation is used to create dependencies in TestNG:

Conclusion

As you can see from JUnit and TestNG's comparison chart, all features are the same except the dependency test. The purpose of both JUnit4 and TestNG testing based frameworks are alike, but the difference is in their use of syntax. Overall, you can choose any of them for unit testing, provide flexibility, and fulfill customer requirements.

author

Ashish Sethi

works as QA Engineer at BugRaptors. He has good experience in manual testing, web application testing, mobile application testing, regression testing, smoke testing, sanity testing, and database testing (Back-End). Also he is able to create required documents for testing like test case checklists, time estimation required for the new projects etc.

Most Popular

Tech Talks With Aparna A Gopalakrishnan

25-Jul-2022 Tech Talks With Aparna A Gopalakrishnan
Read more

Future Of Manufacturing With IoT: Understanding The Opportunities And Challenges

22-Feb-2022 Future Of Manufacturing With IoT: Understanding The Opportunities And Challenges
Read more

The Future Of Manufacturing Industry & Its Emerging Tech Landscape

18-Feb-2022 The Future Of Manufacturing Industry & Its Emerging Tech Landscape
Read more

Interested to share your

QA Requirement!

Tags

  • Junit vs TestNG
  • TestNG vs JUnit
  • how to install junit
  • junit 4 download
  • Sign up for newsletter !


    Comments

    No comments yet! Why don't you be the first?
    Add a comment

    Join our community
    of 1000+ readers.

    To get the latest blogs and techniques on software testing & QA Industry.

    *By entering your email, you subscribe to receive marketing uplates from Bugraptors.You can unsubscribe at any time. For more info, read BugRaptors Privacy Policy.