Create JUnit Test Cases using Maven Project

In this article, we will learn how to write a unit test. We will create a simple maven project to demonstrate how to create JUnit test cases.

We don't use any IDE to create a maven project instead we will use the command line to create a simple maven project. If you want to know how to create a maven project using Eclipse IDE then refer to the How to Create a Simple Maven Project in Eclipse tutorial.

1. Create Simple Maven Project

Let's create a simple maven project by executing the below command:
mvn archetype:generate
-DgroupId=org.yourcompany.project
-DartifactId=application
After the maven project builds success, maven will create a default folder structure.

2. Project Packaging Structure

src
  • src/main/java – This folder contains Java source code packages and classes
  • src/main/resources – This folder contains non-java resources, such as property files and Spring configuration
Test
  • src/test/java – This folder contains the test source code packages and classes
  • src/test/resources – This folder contains non-Java resources, such as property files and Spring configuration
── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── javadevelopersguide
    │   │           └── junit
    │   │               └── Calculator.java
    │   ├── resources
    └── test
        ├── java
        │   └── com
        │       └── javadevelopersguide
        │           └── junit
        │               └── CalculatorTest.java
        └── resources

3. Update JUnit Dependencies in pom.xml File

Let's add below JUnit dependency to our maven project. Open the pom.xml file and add below dependency in the dependencies section:
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

4. Create Calculator.java class

Let's create a Calculator class with some logic so that we will unit test it using JUnit framework:
public class Calculator {
    public int evaluate(String expression) {
        int sum = 0;
        for (String summand : expression.split("\\+"))
            sum += Integer.valueOf(summand);
            return sum;
    } 
}

Create a JUnit Test for Calculator Clas

Two steps required to create a simple test case.
  1. Create a method and annotate a method with @org.junit.Test
  2. When you want to check equality, import org.junit.Assert.* statically, call assertEquals(), and pass expected and actual values.
Here is the complete code:
import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class CalculatorTest {
     @Test
     public void evaluatesExpression() {
        Calculator calculator = new Calculator();
        int sum = calculator.evaluate("1+2+3");
        assertEquals(6, sum);
     }
}

5. Run the test

Run the JUnit test with below maven command using command line:
mvn test
Output:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building junit-getting-started 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ junit-getting-started ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ junit-getting-started ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Git_Work\junit-developers-guide\junit-getting-started\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ junit-getting-started ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ junit-getting-started ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Git_Work\junit-developers-guide\junit-getting-started\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ junit-getting-started ---
[INFO] Surefire report directory: E:\Git_Work\junit-developers-guide\junit-getting-started\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.developersguide.junit.CalculatorTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.069 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.022 s
[INFO] Finished at: 2018-06-27T21:49:29+05:30
[INFO] Final Memory: 11M/28M
[INFO] ------------------------------------------------------------------------

Conclusion

In this article, we have learned how to write a simple JUnit test case by creating a maven project. You can apply more asserts to these examples and have a hands-on experience. The source code available for this guide on GitHub.
GitHub Repository: JUnit Developers Guide

Comments