Environment SetUp for JUNIT with Example

Environment Set Up for JUNIT with Example. In this article, we will see How to configure or set up your first JUNIT Test case.

How to Set Up the Environment?

For a beginner, I am going to tell you about How to set up the environment and start writing the test cases from scratch. Following are the tools you will need:

  • JDK(Java Development Kit)
  • IDE for Java – We will be using Eclipse IDE

JDK(Java Development Kit)

JDK is a development environment for building applications using Java Programming language. It includes tools useful for developing and testing programs.
Download JDK https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html. After Downloading JDK, it will ask you to install JRE(Java Runtime Environment) to install it. Now You have to set up some Environment variables in your OS.

  • Go to My Computer, Right Click on it. Click Properties.
  • Click on Advanced System Setting
  • Click on Environment Variable.
  • In The System Variable, Click on Path and edit it.
  • Add the java installation path to the path variable like “C:\Program Files\jdk1.8.0_20\bin” (it should be a double quote)
  • Click OK and Ok.
  • Just restart your PC
  • Now Open cmd prompt and type “Java version” (for checking whether it is installed)
  • It will return the Java Version else you need to follow these steps again.

IDE Installation (Eclipse IDE)

IDE is an Integrated Development Environment in which java programs are going to be executed. There are many IDE’s available like IntelliJ IDEA, NetBeans, Eclipse, etc. which are open source. Here we are going to use Eclipse. You can get Eclipse from http://www.eclipse.org.

  • Download as per your OS(32 bit or 64 bit).
  • Unzip the file into some location after downloading it.
  • Go to that folder and double-click on eclipse.exe.
  • Now Eclipse will prompt and ask you for the workspace folder(Where all your files will be saved ).
  • It will take time and Eclipse will start.

Thus, these are the only tools that you will require to get started with the JUnit framework.

How to write JUnit Test with Example?

Here, we will write create a Maven Project, and also we will look at How to get started with the JUnit Testing Framework. Thus, follow each step as given below.

Step 1: Start Eclipse IDE.

Step 2: Now, Go to File >New > Maven Project

Step 3: Now, you will get a New Maven Project window.

Step 4: Now, configure the project as shown in the below image. Enter group id, artifact id, and click finish.

Step 5: Now, you will get the project structure in the project explorer of your IDE. Just create a dummy class like StringUtil(the class that we will test)

Step 6: Now, we will create Our First Junit Test Case. Here, we will keep our test file in a separate package. Mostly, we keep the test case file inside src/test/java. After that, click Finish.

Now, you will get the following alert box asking JUnit 4 is not on the build path. If you click OK it will automatically add the Junit 4 library to the build path or else you can add the JUnit dependency in the pom.xml file. Here, now we will click on OK.

Now, you will get the default test case file generated:

  • Here, the default test method is generated automatically.
  • @Test specifies that it is a test case in a Junit class file.
  • Here, the fail() method will automatically fail the Unit test case.

Step 7: Now, Right-click on it and run as Junit Test and see the following output

We can see here the test case is failing. On the left side, we have the red bar that says the test is failing with a failure stack trace below it showing the reason.

Now, we will modify the code in the test() method

package in.codedec.junit.util;

import static org.junit.Assert.*;

import org.junit.Test;

public class StringUtilTest {

    @Test
    public void test() {
        assertEquals(125, 125);
    }

}
  • Here, we have used the assertEquals() method to check whether both the values are the same are not.
  • The first value is the expected value and the second value is the actual value.
  • Expected value is the value that we want the method to return and the actual value is the value that the method is returning.
  • As both the value is the same, then there will be no failures and this test case will be passed successfully.

Step 7: Now again, Right-click on it and run as Junit Test and see the following output

Here, the test is passed with 0 Failures. As we can see the bar is green now.

Thus, this was all about the First JUnit Test case. In the next article, we will continue with this example.