Programming for fun and profit

Programming tutorials, problems, solutions. Always with code.

JUnit Getting Started

How start with JUnit, setup and write basic test? In this post we’ll show how to start with the most popular unit testing library in 5 minutes! Let’s test!

Setup in Maven 3

Add junit:junit dependency with scope test to the project configuration and Maven will pick it up automatically. What more Maven will also run its Surefire plugin to run JUnit tests found in default src/test/java directory.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""



    <!-- the important part: -->
            <!-- use the latest version: -->
            <!-- we want it only for tests: -->



Setup in Gradle

Gradle configuration is very similar – build.gradle:

apply plugin: 'java'

jar {
    baseName = 'junit'
    version =  '0.1.0'

repositories {

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    testCompile group:'junit', name:'junit', version:4.12

Basic test

Let’s suppose that we would like to test the following class, located in src/main/java/com/farenda/junit:

package com.farenda.junit;

public class User {

    private final String firstName;
    private final String lastName;

    public User(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;

    public String getFirstName() {
        return firstName;

    public String getLastName() {
        return firstName;

To do that we can write the following JUnit test and put it in src/test/java/com/farenda/junit. Maven and Gradle will automatically pick up from this location classes ending with Test.

package com.farenda.junit;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class UserTest {

    public void userShouldHaveGivenName() {
        User user = new User("Michael", "Jordan");

        assertEquals("Michael", user.getFirstName());
        assertEquals("Jordan", user.getLastName());

Every test have to be annotated with @Test annotation to let JUnit know that it is a test. To verify results JUnit provides Assert class, which contains many static helper methods. The most common one is assertEquals, which we use here – it takes two arguments, compares them and throws an exception when they are not equal.

The above code has one bug which will be reported by JUnit as here:

Expected :Jordan
Actual   :Michael

at org.junit.Assert.assertEquals(
at org.junit.Assert.assertEquals(
at com.farenda.junit.UserTest.userShouldHaveGivenName(

How Pros do it

The following code is part of from Facebook Fresco project:

package com.facebook.common.util;

import org.junit.Test;

import static org.junit.Assert.*;

/** Unit test for {@link TriState}. */
public class TriStateTest {

  //... other tests commented out for brevity

  public void testValueOf() {
    assertEquals(TriState.YES, TriState.valueOf(true));
    assertEquals(TriState.NO, TriState.valueOf(false));

  //... other tests commented out for brevity

Have other interesting examples? Let us know in the comments! :-)

Share with the World!