How to Use JUnit Library in Java Project?

JUnit caters the need to test small/large chunks of code. It is an open source unit testing tool to evaluate the test results. It is very easy to run a JUnit test and doesn’t require you to create class object or define main method.

In order to add JUnit library, you need to follow the below-mentioned steps.

  1. Go the Java project and right-click.
  2. Go to Build Path->Configure Build Path
  3. Click ‘Libraries’.Libraries
  4. Click ‘Add Library’.
  5. You will get the ‘Add Library’ window.Add Library Window
  6. Select ‘JUnit’ and click ‘Next’.
  7. You will get a screen to select the JUnit version to be used in the project.JUnit Version
  8. Select JUnit library version as JUnit4 and click ‘Finish’.
  9. Click ‘OK’.Library Added

 

JUnit tool is being used as a test runner by various frameworks, including Data Driven Framework, Keyword Driven Framework, Hybrid Framework, and more.

JUnit Annotations

You can run the test method by using various annotations available in JUnit. Some of the frequently used annotations in Selenium scripts and framework are mentioned below.

  1. @Test

It is used to run a JUnit test.

  1. @Before

It is used to run any particular test before each test.

  1. @BeforeClass

BeforeClass method can be used to initialize properties files, database, etc. The method is static and executes once before running all tests.

  1. @After

It executes after each test.

  1. @AfterClass

This is a static method and runs once after executing all test methods.

  1. Parameterized JUnit class

It is used to run the same scenario with multiple data set.

You may also like: Open Source Web Testing Tools in Java

 

SHARE THIS

Get A Free Quote