How to setup JUnit testing in Eclipse

Ayush Verma
3 min readFeb 16, 2019

--

JUnit 4

Worried about the correctness of your Java code? JUnit is here for the rescue. JUnit is a unit testing framework for the the java programming language to write repeatable tests. JUnit is very important and useful in test-driven development. JUnit provides mechanism to create failure cases for your code and to further address them correctly. JUnit is inbuilt in Java. The latest version is JUnit 5. I’ll be using JUnit 4 to show you how to setup it up from scratch in Eclipse since JUnit 5 is pretty new and not used widely.

1. Importing JUnit framework into your project

  1. Right click on your project in the Package Explorer -> Properties.
  2. Under Java Build Path, click on Add Library.

3.Inside the Add Library dialog box, select JUnit.

4.Click Next and Finish.

5. Apply & Close.

6. Now you’ll see JUnit framework included in your project directory.

2. Creating a testing file

  1. Right click on your package and select New -> JUnit Test Case.

2. This will open a New JUnit Test Case dialog box. Enter some to create a testfile and check the setUp() method in the same dialog box. setUp() method runs every time you run your test file and creates objects to test on your code. Click Finish.

3. Now you’ll see a test file which you just created like this below.

3. A Sample Test Case Example

I have created a Doubly Linked List code.

setUp() method for DoublyLinkedList tester file.

Method to test on.

Successful tests are shown in green and hence your code is working correctly.

So that’s how you do unit testing in Java in eclipse. Unit testing ensures your code is not error prone when you finally launch it.

--

--