Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

JUnit Test Runners

farenda 2015-12-31 0

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;

@RunWith(RobolectricTestRunner.class)
public class HashCodeUtilTest {

  @Test
  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) {
    assertEquals(
        Objects.hashCode(o1),
        HashCodeUtil.hashCode(o1));
    assertEquals(
        Objects.hashCode(o1, o2),
        HashCodeUtil.hashCode(o1, o2));
    assertEquals(
        Objects.hashCode(o1, o2, o3),
        HashCodeUtil.hashCode(o1, o2, o3));
    assertEquals(
        Objects.hashCode(o1, o2, o3, o4),
        HashCodeUtil.hashCode(o1, o2, o3, o4));
    assertEquals(
        Objects.hashCode(o1, o2, o3, o4, o5),
        HashCodeUtil.hashCode(o1, o2, o3, o4, o5));
    assertEquals(
        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
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TestPropertySource("explicit.properties")
public class ExplicitPropertiesFileTestPropertySourceTests {

      @Autowired
      protected Environment env;


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


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

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

}
Share with the World!
Categories JUnit Tags java, junit, unit-tests
Previous: JUnit Matchers CheatSheet
Next: JUnit Test Suites

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok