# Replace JUnit Assumptions with Hamcrest JUnit

# Description

This rule replaces the JUnit 4 assumptions Assume.assumeThat (opens new window), Assume.assumeNoException (opens new window), and Assume.assumeNotNull (opens new window) by the equivalent invocations of Hamcrest JUnit assumption MatcherAssume.assumeThat (opens new window).
Since JUnit 5 contains no equivalent assumption methods, this rule eliminates an obstacle for migration to JUnit 5.

Requirements

This rule requires the following libraries to be available in the project's classpath:

  • org.hamcrest:hamcrest-core:1.3
  • org.hamcrest:hamcrest-junit:1.0.0.0

# Benefits

Helps migrating to JUnit 5 by removing references to JUnit 4 methods.

# Tags

# Code Changes

# Invocation of assumeThat without Message

Pre

import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.junit.Assume.assumeThat;

import org.junit.jupiter.api.Test;

public class AssumptionExampleTest {
	@Test
	public void test() {
		//...
		assumeThat(value, equalToIgnoringCase("value"));
	}
}

Post

import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.junit.MatcherAssume.assumeThat;

import org.junit.jupiter.api.Test;

public class AssumptionExampleTest {
	@Test
	public void test() {
		//...
		assumeThat(value, equalToIgnoringCase("value"));
	}
}

# Invocation of assumeThat with Message

Pre

import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.junit.Assume.assumeThat;

import org.junit.jupiter.api.Test;

public class AssumptionExampleTest {
	@Test
	public void test() {
		//...
		assumeThat("Assuming case insensitive \"value\".", 
			value,
			equalToIgnoringCase("value"));
	}
}

Post

import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.junit.MatcherAssume.assumeThat;

import org.junit.jupiter.api.Test;

public class AssumptionExampleTest {
	@Test
	public void test() {
		//...
		assumeThat("Assuming case insensitive \"value\".", 
			value,
			equalToIgnoringCase("value"));
	}
}

# Invocation of assumeThat with Qualifier

Pre

import static org.hamcrest.Matchers.equalToIgnoringCase;

import org.hamcrest.Matcher;
import org.junit.Assume;
import org.junit.jupiter.api.Test;

public class AssumptionExampleTest {

	static <T> void assumeThat(T actual, Matcher<T> matcher) {
		//...
	}

	@Test
	public void test() {
		//...
		Assume.assumeThat(value, equalToIgnoringCase("value"));
	}
}

Post

import static org.hamcrest.Matchers.equalToIgnoringCase;

import org.hamcrest.Matcher;
import org.hamcrest.junit.MatcherAssume;
import org.junit.jupiter.api.Test;

public class AssumptionExampleTest {
	static <T> void assumeThat(T actual, Matcher<T> matcher) {
		//...
	}

	@Test
	public void test() {
		//...
		MatcherAssume.assumeThat(value, equalToIgnoringCase("value"));
	}
}

# Invocation of assumeNoException with Message

Pre

import static org.junit.Assume.assumeNoException;

import org.junit.jupiter.api.Test;

public class AssumptionExampleTest {

	@Test
	public void test() {
		//...
		assumeNoException("No exception is thrown.", exception);
	}
}

Post

import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.junit.MatcherAssume.assumeThat;

import org.junit.jupiter.api.Test;

public class AssumptionExampleTest {
	
	@Test
	public void test() {
		//...
		assumeThat("No exception is thrown.", exception, nullValue());
	}
}

# Invocation of assumeNotNull with Multiple Objects

Pre

import static org.junit.Assume.assumeNotNull;

import org.junit.jupiter.api.Test;

public class AssumptionExampleTest {

	@Test
	public void test() {
		//...
		assumeNotNull(o1, o2);
	}
}

Post

import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.everyItem;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.junit.MatcherAssume.assumeThat;

import org.junit.jupiter.api.Test;

public class AssumptionExampleTest {

	@Test
	public void test() {
		//...
		assumeThat(asList(o1, o1), everyItem(notNullValue()));
	}
}

# Invocation of assumeNotNull with Single Object

Pre

import static org.junit.Assume.assumeNotNull;

import org.junit.jupiter.api.Test;

public class AssumptionExampleTest {
	@Test
	public void test() {
		//...
		assumeNotNull(o);
	}
}

Post

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.junit.MatcherAssume.assumeThat;

import org.junit.jupiter.api.Test;

public class AssumptionExampleTest {

	@Test
	public void test() {
		//...
		assumeThat(o, notNullValue());
	}
}

# Invocation of assumeNotNull with Object Array Variable

Pre

import static org.junit.Assume.assumeNotNull;

import org.junit.jupiter.api.Test;

public class AssumptionExampleTest {
	Object[] objects;

	@Test
	public void test() {
		//...
		assumeNotNull(objects);
	}
}

Post

import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.everyItem;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.junit.MatcherAssume.assumeThat;

import org.junit.jupiter.api.Test;

public class AssumptionExampleTest {
	Object[] objects;

	@Test
	public void test() {
		//...
		assumeThat(objects, notNullValue());
		assumeThat(asList(objects), everyItem(notNullValue()));
		
	}
}

🛠️ Auto-refactor Available

You can auto refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

Need help? Check out our installation guide.

# Properties

Property Value
Rule ID ReplaceJUnit4AssumptionsWithHamcrestJUnit
First seen in jSparrow version 4.0.0
Minimum Java version 5
Remediation cost 2 min