Quick revision
The short answer
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.
What to remember
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.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
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.
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.
| Tool | Evaluated | Result when false |
|---|---|---|
assertTrue | inside the test | test failed |
assumeTrue | inside the test | rest of test aborted, reported as skipped |
assumingThat | inside the test | only that block skipped, test continues |
@EnabledOnOs and friends | before the test, by an ExecutionCondition | test 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.
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.
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/