JUnit Getting Started Guide

Overview

This getting started guide shows you how to write a unit test. We will create a simple maven project to demonstrate how to create JUnit test cases.

Create Simple Maven Project

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

Project Packaging Structure

  • src/main/java – Java Source code packages and classes
  • src/main/resources – NON-Java Resources, such as property files and Spring configuration
Test
  • src/test/java – Test Source code packages and classes
  • src/test/resources – 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

Update JUnit Dependencies in pom.xml File

<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.12</version>
 <scope>test</scope>
</dependency>

Create Calculator.java class

package com.developersguide.junit;

public class Calculator {
 public int evaluate(String expression) {
  int sum = 0;
  for (String summand : expression.split("\\+"))
   sum += Integer.valueOf(summand);
  return sum;
 }

}

Create a test

Two steps required to create a simple test case.
  1. 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.
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);
  }
}

Run the test

Run the test using maven from the 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 guide, we have learned the how to write simple JUnit test case by creating a maven project. You can apply more asserts to this examples and have a hands-on experience. The source code available for this guide on GitHub.
On GitHub, there is a multi-module project and this getting started project is a module of it.
GitHub Repository: JUnit Developers Guide

Comments