Creating a JUnit Test Case in Eclipse

by YusufT13 in Circuits > Software

9475 Views, 0 Favorites, 0 Comments

Creating a JUnit Test Case in Eclipse

Finished.png

To test Java code in Eclipse, the programmer must write his/her own tests. JUnit Tests are most often used by experienced programmers to verify the correctness and efficiency of their code. This style of testing is taught in most Universities, such as DePaul University, and is encouraged to be used by students to test their homework solutions. The level of difficulty of creating the test cases is minimal, however creating the JUnit Test file is difficult for any beginner. An example JUnit Test file is pictured.

Open Eclipse

Step1.PNG

Click on the Eclipse Java Neon Icon on the Desktop, or search for Eclipse in the search Box.

Properties

Step2.PNG

On the top navigation bar, Click Project. In the drop-down menu, click Properties.

Java Build Path

Step3.PNG

First, Click on ‘Java Build Path’ on the left-hand side. Then, click on ‘Libraries’ on the right side near the top.

Add Library

Step4.JPG

Click on the ‘Add Library…’ Icon on the right-hand side.

JUnit

Step5.JPG

First, Click ‘JUnit’ to make sure it’s highlighted. Then, Click the ‘Next >’ Icon on the bottom.

JUnit Version

Step6.JPG

Click on the dropdown menu next to ‘JUnit library version:’. Select the Latest Version available, i.e. JUnit 4 or JUnit 5. Then click 'Finish'.

Apply the Changes

Step7.JPG

Click on 'Apply and Close' near the bottom right of the window.

New JUnit Test File

Step8.JPG

Being on the main screen in Eclipse, Click on File, New, JUnit Test Case using the drop-down menu.

Finish Creating the File

Step9.JPG

Next to the ‘Name:’ Box, write in the name of the test file. A default file name is written by Eclipse, but this can be changed. Then, Click ‘Finish’ at the bottom.