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.
- Go the Java project and right-click.
- Go to Build Path->Configure Build Path
- Click ‘Libraries’.
- Click ‘Add Library’.
- You will get the ‘Add Library’ window.
- Select ‘JUnit’ and click ‘Next’.
- You will get a screen to select the JUnit version to be used in the project.
- Select JUnit library version as JUnit4 and click ‘Finish’.
- Click ‘OK’.
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.
- @Test
It is used to run a JUnit test.
- @Before
It is used to run any particular test before each test.
- @BeforeClass
BeforeClass method can be used to initialize properties files, database, etc. The method is static and executes once before running all tests.
- @After
It executes after each test.
- @AfterClass
This is a static method and runs once after executing all test methods.
- 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