Java Programming Tutorials

Java programming tutorials with many code examples!

JUnit Assume – execute tests conditionally

JUnit Assume is a handy feature that allows developer to specify assumptions about execution environment and hence execute tests conditionally. It’s practical and easy to use.

Sometimes it happens that you have to write a test that works only on a specific operating system (e.g. Linux) or in some environment you don’t control (e.g. Continuous Integration server). This way or another you would like to execute/disable it when some conditions are met. JUnit Assumptions is the feature that serves exactly that purpose.

To state conditions for your tests you need to use static methods from org.junit.Assume class as in the following example. When JUnit’s test runner will run a test it will execute assumption (e.g. assumeThat method) and will execute the rest of the test when the assumption is met, else it will ignore the test!

org.junit.Assume class provides only a handful of methods, because it’s meant to be used with CoreMatchers – see the post about JUnit Matchers if you need a refresher!

package com.farenda.junit;

import org.junit.Test;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.*;

public class AssumptionsTest {

    public void shouldRunOnlyOnLinux() {
        assumeThat(System.getProperty(""), is("Linux"));
        assertThat(Paths.get("/tmp").toString(), equalTo("/tmp"));

    public void shouldRunOnlyWhenOneIsOne() {
        assumeTrue("Expected true!", 1 == 1);
        assertThat(1 + 1, is(2));

    public void shouldRunOnlyWhenFalse() {
        assumeFalse("Expected false!", 1 == 2);
        assertThat(Math.pow(2, 3), is(8.0));

    public void shouldRunOnlyWhenCanOpenFile() {
        List<String> data = null;
        try {
            data = Files.readAllLines(Paths.get("/proc/meminfo"));
        } catch (IOException e) {
        assertThat(data.isEmpty(), is(false));

Cool thing is that assumptions can be specified on fixtures too, so you can put them in @Before/@After and @BeforeClass/@AfterClass methods to include/exclude all the tests! Nice. :-)

How pros do it

Let’s see an example from Spring FrameworkLog4jWebConfigurerTests:

package org.springframework.web.util;

import org.junit.Test;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.*;

// other imports cut for brevity

 * @author Juergen Hoeller
 * @author Sam Brannen
 * @since 21.02.2005
public class Log4jWebConfigurerTests {

    // ... other tests cut for brevity

    public void initLoggingWithAbsoluteFilePathAndRefreshInterval() {
        // Only works on MS Windows
        assumeThat(System.getProperty(""), containsString("Windows"));
        URL url = Log4jWebConfigurerTests.class.getResource(TESTLOG4J_PROPERTIES);
        initLogging(url.getFile(), true);

In the next example, this time ServiceBootstrappingTest from Hibernate ORM, we can see that a test is executed only if SHOW_SQL property hasn’t been passed:

package org.hibernate.test.service;

// imports cut for brevity

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Properties;

import org.junit.Assume;
import org.junit.Test;

 * @author Steve Ebersole
@RequiresDialect( H2Dialect.class )
public class ServiceBootstrappingTest extends BaseUnitTestCase {
    public void testBasicBuild() {
        // this test requires that SHOW_SQL property isn't passed from the outside (eg. via Gradle)
        final String showSqlPropertyFromOutside = System.getProperty(Environment.SHOW_SQL);

        final StandardServiceRegistryImpl serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder()
                .applySettings( ConnectionProviderBuilder.getConnectionProviderProperties() )
        final JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class );

        assertTrue( jdbcServices.getDialect() instanceof H2Dialect );

        final ConnectionProviderJdbcConnectionAccess connectionAccess = assertTyping(
        assertTrue( connectionAccess.getConnectionProvider().isUnwrappableAs( DriverManagerConnectionProviderImpl.class ) );
        assertFalse( jdbcServices.getSqlStatementLogger().isLogToStdout() );


    // other tests cut for brevity

We live in beautiful times when we can learn a lot from masters! Thanks! :-)

Share with the World!