Programming for fun and profit

Programming tutorials, problems, solutions. Always with code.

JUnit Test Runners

What are JUnit Test Runners and how to use them? In this post we present this powerful JUnit’s extension mechanism. Learn how to use it like a pro!

Test Runners is JUnit‘s mechanism that actually executes tests. In most cases it’s fine when JUnit executes tests using standard org.junit.runners.JUnit4 runner, but sometimes we would like to execute tests in some other way. Test runners allow to do that.

The most common test runners are:

  • org.junit.runners.Suite
    Runs suites of tests (we will cover in further posts).
  • org.junit.runners.Parameterized (we will cover in further posts)
    Runs tests with parameters
  • org.mockito.runners.MockitoJUnitRunner
    Used to run tests with Mockito mocking framework.
  • org.powermock.modules.junit4.PowerMockRunner
    Used to run tests with PowerMock mocking framework.
  • org.robolectric.RobolectricTestRunner
    Used to run tests with Robolectric that provides simulated Android environment for tests.
  • org.springframework.test.context.junit4.SpringJUnit4ClassRunner
    Used to run tests with Spring Framework enabled.

To run tests with a test runner we use JUnit’s @RunWith annotation.

How pros do it

In the following example we see how Facebook’s Fresco is using RobolectricTestRunner to run Android tests:

package com.facebook.common.util;

import org.robolectric.RobolectricTestRunner;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

public class HashCodeUtilTest {

  public void testSimple() {
    testCase(1, 2, 3, 4, 5, 6);

  // other parts removed for brevity

  private void testCase(Object o1, Object o2, Object o3, Object o4, Object o5, Object o6) {
        Objects.hashCode(o1, o2),
        HashCodeUtil.hashCode(o1, o2));
        Objects.hashCode(o1, o2, o3),
        HashCodeUtil.hashCode(o1, o2, o3));
        Objects.hashCode(o1, o2, o3, o4),
        HashCodeUtil.hashCode(o1, o2, o3, o4));
        Objects.hashCode(o1, o2, o3, o4, o5),
        HashCodeUtil.hashCode(o1, o2, o3, o4, o5));
        Objects.hashCode(o1, o2, o3, o4, o5, o6),
        HashCodeUtil.hashCode(o1, o2, o3, o4, o5, o6));

Another one comes from Spring Framework and show how to enable Spring Framework in tests to have Dependency Injection and be able to test Spring Context:

package org.springframework.test.context.env;

import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.*;

 * Integration tests for {@link TestPropertySource @TestPropertySource}
 * support with an explicitly named properties file.
 * @author Sam Brannen
 * @since 4.1
public class ExplicitPropertiesFileTestPropertySourceTests {

      protected Environment env;

      public void verifyPropertiesAreAvailableInEnvironment() {
              String userHomeKey = "user.home";
              assertEquals(System.getProperty(userHomeKey), env.getProperty(userHomeKey));
              assertEquals("enigma", env.getProperty("explicit"));

      // -------------------------------------------------------------------

      static class Config {
              /* no user beans required for these tests */

Share with the World!