Assumptions Flashcards
Assumptions
Assumptions are typically used whenever it does not make sense to continue the execution of a given test method, for example, if the test depends on something that does not exist in the current runtime environment. In the test report, these tests will be marked as aborted.
Difference b/w Assumptions and Assertions
Assert to fail a test if something goes wrong
A failed assumption means you don’t run the test because something you assumed to be true is not true and so you just skip the test.
Assumptions.abort()
The abort() method aborts the test with or without a message.
Assumptions.assumeTrue()
@Test
void testOnDev() {
System.setProperty(“ENV”, “DEV”);
Assumptions.assumeTrue(“DEV”.equals(System.getProperty(“ENV”)));
//remainder of test will proceed
}
@Test void testOnProd(){ System.setProperty("ENV", "PROD"); Assumptions.assumeTrue("DEV".equals(System.getProperty("ENV")), AppTest::message); //remainder of test will be aborted }
Assumptions.assumeFalse()
The assumeFalse() method validates the given assumption to false and if the assumption is false – test proceed, otherwise, test execution is aborted.
Assertions.assumingThat()
This method executes the supplied Executable, but only if the supplied assumption is valid.