# Replace JUnit 4 Assumptions with JUnit Jupiter

# Description

This rule contributes to a stepwise transition to JUnit 5 by replacing the JUnit 4 assumption methods by the equivalent JUnit 5 ones. In JUnit 5, all methods used to carry out assumptions are declared in org.junit.jupiter.api.Assumptions (opens new window), while in JUnit 4 the equivalent methods are declared in org.junit.Assume (opens new window). This rule looks for invocations of overloaded methods of the JUnit 4 class Assume and replaces them with the invocations of equivalent overloaded alternatives declared in the JUnit 5 class Assumptions:

Assertions in Junit 4 org.junit.Assume Assertions in JUnit 5 org.junit.jupiter.api.Assumptions
Assume.assumeFalse (opens new window) Assumptions.assumeFalse (opens new window)
Assume.assumeTrue (opens new window) Assumptions.assumeTrue (opens new window)

By replacing each of these JUnit 4 Assume methods by the corresponding Jupiter alternative, this rule promotes a stepwise transition to JUnit 5.

Requirements

This rule requires the following libraries to be present:

  • junit:junit:4.13
  • org.junit.jupiter:junit-jupiter-api:5.4.0

# Benefits

Migrates JUnit 4 tests to JUnit 5.

# Tags

# Code Changes

# Replacing Imports

Pre

import static org.junit.Assume.assumeTrue;
//...
public class OrderTest {
	//...
	@Test
	public void testService() {
		assumeTrue(orderService.isAvailableInStock("1", 5));
		Order order = orderService.book("1", 5, "user-id");
		assertNotNull(order);
	}
}

Post

import static org.junit.jupiter.api.Assumptions.assumeTrue;
//...
public class OrderTest {
	//...
	@Test
	public void testService() {
		assumeTrue(orderService.isAvailableInStock("1", 5));
		Order order = orderService.book("1", 5, "user-id");
		assertNotNull(order);
	}
}

# Replacing Invocations with a Message

Pre

import static org.junit.Assume.assumeTrue;
//...
public class OrderTest {
	//...
	@Test
	public void testService() {
		assumeTrue("If product 1 is available", orderService.isAvailableInStock("1", 5));
		Order order = orderService.book("1", 5, "user-id");
		assertNotNull(order);
	}
}

Post

import static org.junit.jupiter.api.Assumptions.assumeTrue;
//...
public class OrderTest {
	//...
	@Test
	public void testService() {
		assumeTrue(orderService.isAvailableInStock("1", 5), "If product 1 is available");
		Order order = orderService.book("1", 5, "user-id");
		assertNotNull(order);
	}
}

# Replacing the Assume Qualifier

Pre

import static org.assumeMethods.AssumeMethods.assumeTrue;
import org.junit.Assume;
//...
public class OrderTest {
	//...
	@Test
	public void testService() {
		Assume.assumeTrue(orderService.isAvailableInStock("1", 5));
		Order order = orderService.book("1", 5, "user-id");
		assertNotNull(order);
	}
}

Post

import static org.assumeMethods.AssumeMethods.assumeTrue;
import org.junit.jupiter.api.Assumptions;
//...
public class OrderTest {
	//...
	@Test
	public void testService() {
		Assumptions.assumeTrue(orderService.isAvailableInStock("1", 5));
		Order order = orderService.book("1", 5, "user-id");
		assertNotNull(order);
	}
}

Use a Java Refactoring Tool

No license required

You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.

System-wide Refactoring

Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.

a drawn cute bird pointing at a graph that shows positive results

# Properties

Property Value
Rule ID ReplaceJUnit4AssumptionsWithJupiter
First seen in jSparrow version 3.30.0
Minimum Java version 8
Remediation cost 2 min