Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is the difference between an assertion failure and an assumption failure in JUnit 5? Show Assumptions.assumeTrue and assumingThat, and two annotations that enable a test only in some environments.

A failed assertion marks a test as failed, while a failed assumption aborts the test and it is reported as skipped (aborted), not failed. assumeTrue(System.getenv("CI") != null) stops the rest of the test when the condition is false, whereas assumingThat(condition, executable) runs only its block conditionally and then continues the test. Annotations such as @EnabledOnOs(OS.LINUX), @EnabledForJreRange, @EnabledIfEnvironmentVariable, @EnabledIfSystemProperty and @DisabledIf("methodName") enable or disable a test before it runs, and custom rules can be written as an ExecutionCondition extension.

Why this question is asked

Some tests only make sense in some environments: a test for a Linux-only file permission, a test that needs a real Docker daemon, a test for a Java 25 feature. We need a way to say not applicable here without pretending the test passed or failed. Interviewers ask this to check that we know the difference between failing and skipping, and the different tools JUnit gives for it. In this answer, we will see how assertions and assumptions report differently, use assumeTrue and assumingThat, look at the conditional annotations, see when each is evaluated, write a custom condition, and discuss the risk of skipped tests in CI.

1Failed Assertions Against Failed Assumptions

An assertion checks that the code behaves correctly. If it fails, JUnit throws an AssertionFailedError and the test is reported as failed: something is wrong. An assumption checks that the environment is suitable for the test. If it fails, JUnit throws a TestAbortedException, stops the test, and reports it as aborted, which build tools and IDEs show as skipped. The message is different: not the code is broken, but this test could not run here.

So assumptions must never be used to check behaviour. An assumption that the result is positive would quietly skip the test exactly when the code is wrong.

2Using assumeTrue and assumingThat

assumeTrue takes a condition and an optional message. When the condition is false, the rest of the test does not run. assumingThat takes a condition and an executable: it runs that block only when the condition is true and then continues with the rest of the test either way, so it narrows one part of a test instead of skipping all of it.

Java
import static org.junit.jupiter.api.Assumptions.*;
@Test void uploadsToRealBucket() {
assumeTrue(System.getenv("CI") != null, "only runs on CI, which has bucket credentials");
// on a laptop without CI set: the test stops here and is reported as skipped
storage.upload("report.csv", bytes);
assertTrue(storage.exists("report.csv"));
}
@Test void formatsPath() {
assumingThat(System.getProperty("os.name").startsWith("Windows"),
() -> assertEquals("C:\\data\\in", paths.format("data/in"))); // only on Windows
assertNotNull(paths.format("data/in")); // always runs
}

Notice that formatsPath is never skipped; on Linux it simply leaves out the Windows-specific check. There is also assumeFalse, the mirror of assumeTrue.

3Annotations That Enable a Test Only in Some Environments

For conditions that are known before the test starts, annotations are clearer than code. JUnit provides pairs of Enabled and Disabled annotations for the common cases.

Java
@Test @EnabledOnOs(OS.LINUX)
void setsPosixPermissions() { } // skipped on macOS and Windows
@Test @EnabledForJreRange(min = JRE.JAVA_21)
void usesVirtualThreads() { } // skipped on Java 17
@Test @EnabledIfEnvironmentVariable(named = "DOCKER_HOST", matches = ".+")
void runsAgainstContainer() { }
@Test @EnabledIfSystemProperty(named = "run.slow", matches = "true")
void slowReconciliation() { } // run with -Drun.slow=true
@Test @DisabledIf("noLicenceFile")
void usesLicensedEngine() { }
boolean noLicenceFile() { return !Files.exists(Path.of("licence.key")); }
Assertions, assumptions and conditions
ToolEvaluatedResult when false
assertTrueinside the testtest failed
assumeTrueinside the testrest of test aborted, reported as skipped
assumingThatinside the testonly that block skipped, test continues
@EnabledOnOs and friendsbefore the test, by an ExecutionConditiontest skipped, no lifecycle methods run

@EnabledOnJre names specific versions, while @EnabledForJreRange takes a min and max. @EnabledIf and @DisabledIf name a method that returns a boolean, which covers anything the other annotations do not. Each annotation may have a disabledReason attribute, which appears in the report and saves people guessing why a test did not run.

4When Each Kind Is Evaluated

Conditional annotations are evaluated by JUnit before the test runs. If a test is disabled, JUnit does not run its @BeforeEach or @AfterEach methods or its body; if a whole class is disabled, its @BeforeAll does not run either. That makes annotations cheap and safe for tests whose setup itself would fail in the wrong environment.

Assumptions run inside the test, so by the time assumeTrue is called, the instance has been created and @BeforeEach has already run, and @AfterEach still runs afterwards. That is why an assumption is right when the condition can only be computed during setup, for example after trying to connect to a service, and an annotation is right when it is known from the environment.

5Writing a Custom Condition

When the same rule appears in many tests, such as only run where a GPU is present or only in the nightly pipeline, we can write it once as an ExecutionCondition extension and wrap it in our own annotation.

Java
public class NightlyOnlyCondition implements ExecutionCondition {
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
return "nightly".equals(System.getenv("PIPELINE"))
? ConditionEvaluationResult.enabled("nightly pipeline")
: ConditionEvaluationResult.disabled("runs only in the nightly pipeline");
}
}
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(NightlyOnlyCondition.class)
public @interface NightlyOnly { }
@Test @NightlyOnly void fullReconciliation() { } // skipped with reason in every other pipeline

The reason string appears in the report, so the skip is explained rather than silent.

6Follow-up questions

What risk do skipped tests create in CI?

A skipped test looks harmless because the build stays green, but it protects nothing. If a condition is wrong, for example an environment variable was renamed and an assumption now always fails, a whole group of tests can silently stop running for months while the build reports success. So I would track the number of skipped tests per pipeline, alert when it rises, make sure every skip has a reason, and ensure that there is at least one pipeline where each conditional test really runs, such as a Linux agent for Linux-only tests or a nightly job for slow tests.

Should I use @Disabled for a broken test?

Only briefly, with a reason and a ticket, for example @Disabled("flaky, see TICKET-123"). @Disabled has no condition, so the test never runs anywhere, and disabled tests tend to be forgotten. A regular review of disabled tests keeps them from piling up.

Is an assumption failure ever reported as a failure?

Not by JUnit itself; it reports the test as aborted. Some CI dashboards group aborted with skipped. The exception is when the TestAbortedException escapes in a place where it is not expected, such as some lifecycle callbacks in custom extensions, so assumptions belong in tests and @BeforeEach methods.

7Conclusion

In this answer, we saw that a failed assertion fails a test while a failed assumption aborts it and reports it as skipped. assumeTrue stops the rest of the test, assumingThat runs only a block conditionally and continues, and annotations such as @EnabledOnOs, @EnabledForJreRange, @EnabledIfEnvironmentVariable, @EnabledIfSystemProperty and @DisabledIf decide before the test runs, so lifecycle methods of disabled tests do not run. Custom rules become ExecutionCondition extensions, and skipped tests must be tracked in CI so they do not silently stop protecting us.

What to say in the interviewAbout 80 seconds aloud

Open with

An assertion checks the behaviour of the code; if it fails, the test is marked failed. An assumption checks whether the environment is right for the test; if it fails, JUnit throws TestAbortedException and the test is reported as aborted, which shows up as skipped, not failed.

Then, if they want more

For example, assumeTrue(System.getenv("CI") != null) at the start of a test that needs cloud credentials stops the rest of the test on a laptop. assumingThat is narrower: it takes a condition and a lambda, runs the lambda only when the condition is true and then carries on with the rest of the test, so I can have a Windows-only check inside a test that otherwise runs everywhere.

When the condition is known up front, I prefer annotations, like @EnabledOnOs(OS.LINUX), @EnabledForJreRange(min = JRE.JAVA_21), @EnabledIfEnvironmentVariable, @EnabledIfSystemProperty or @DisabledIf with a method name. Those are evaluated before the test runs, so for a disabled test JUnit doesn't even run @BeforeEach, while an assumption runs inside the test after setup.

For a rule many tests share, I write an ExecutionCondition extension behind my own annotation. The risk in CI is that skipped tests keep the build green while testing nothing, so I track skip counts, require reasons, and make sure each conditional test really runs in some pipeline.

About this answer

Last updated
12 November 2026
Versions it names
Java 17, Java 25
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
JUnit user guide
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between an assertion failure and an assumption failure in JUnit...": https://scrivio-interview-library.netlify.app/java/junit/ea27f590b8/