Junit5 Assumptions.

Maheshwar Ligade
techwasti
Published in
1 min readSep 21, 2019

--

Assumptions are used to run tests only if certain conditions are met. This is typically used for external conditions that are required for the test to execute properly, but which are not directly related to whatever is being unit tested.

If the assumeTrue() condition is true, then run the test, else aborting the test.

If the assumeFalse() condition is false, then run the test, else aborting the test.

The assumingThat() is much more flexible, it allows part of the code to run as a conditional test.

When the assumption is false, a TestAbortedException is thrown and the test is aborting execution.

@Test
void trueAssumption() {
assumeTrue(6 > 2);
assertEquals(6 + 2, 8);
}

@Test
void falseAssumption() {
assumeFalse(4 < 1);
assertEquals(4 + 2, 6);
}

@Test
void assumptionThat() {
String str = "a simple string";
assumingThat(
str.equals("a simple string"),
() -> assertEquals(3 + 2, 1)
);
}

If you enjoyed this article, please don’t forget to Clap.

For more stories

Let's connect on Stackoverflow , LinkedIn , Facebook& Twitter.

--

--

Maheshwar Ligade
techwasti

Learner, Full Stack Developer, blogger, amateur #ML,#DL,#AI dev in the quantum moment. I run https://techwasti.com/ to post all my articles.