Assumptions are very useful feature in junit. Using assumption, you can make sure that the test will not execute, if the preconditions/assumptions are not met. That means, the tests that do not satisfy the assumptions are skipped.
For example, below test case will execute in windows systems and skipped in other operating systems.
@Test
public void onlyOnWindows() {
assumeTrue(isWindows());
assertTrue(true);
}
Find the below working application.
AssumptionsDemo.java
package com.sample.app;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import org.junit.jupiter.api.Test;
public class AssumptionsDemo {
@Test
public void onlyOnWindows() {
assumeTrue(isWindows());
assertTrue(true);
}
@Test
public void notOnWindows() {
assumeTrue(!isWindows());
assertTrue(true);
}
private static boolean isWindows() {
String osType = System.getProperty("os.name");
if (osType.contains("windows") || osType.contains("Windows") || osType.contains("WINDOWS")) {
return true;
}
return false;
}
}
When you ran above application, you can observe that one test case gets skipped depends on your operating system.
When I ran on MAC, test method ‘onlyOnWindows’ skipped.
All JUnit Jupiter assumptions are static methods in the org.junit.jupiter.api.Assumptions class. Following are the assumptions supported right now.
public static void assumeTrue(boolean assumption) throws TestAbortedException
Validate the given assumption. 'assumption' specifies the assumption to validate.
public static void assumeTrue(BooleanSupplier assumptionSupplier) throws TestAbortedException
Validate the given assumption. 'assumptionSupplier' the supplier of the assumption to validate
public static void assumeTrue(BooleanSupplier assumptionSupplier, String message) throws TestAbortedException
Validate the given assumption. 'assumptionSupplier' the supplier of the assumption to validate and 'message' specifies the message to be included in the TestAbortedException if the assumption is invalid.
public static void assumeTrue(boolean assumption, Supplier<String> messageSupplier) throws TestAbortedException
Validate the given assumption. 'messageSupplier' specifies the supplier of the message to be included in the TestAbortedException if the assumption is invalid
public static void assumeTrue(boolean assumption, String message) throws TestAbortedException
Validate the given assumption. 'assumption' specifies the assumption to validate and 'message' specifies the message to be included in the TestAbortedException if the assumption is invalid.
public static void assumeTrue(BooleanSupplier assumptionSupplier, Supplier<String> messageSupplier) throws TestAbortedException
Validate the given assumption. 'assumptionSupplier' specifies the supplier of the assumption to validate and 'messageSupplier' specifies the supplier of the message to be included in the TestAbortedException if the assumption is invalid.
public static void assumeFalse(boolean assumption) throws TestAbortedException
Validate the given assumption. 'assumption' specifies the assumption to validate'
public static void assumeFalse(BooleanSupplier assumptionSupplier) throws TestAbortedException
Validate the given assumption. 'assumptionSupplier' specifies the supplier of the assumption to validate.
public static void assumeFalse(BooleanSupplier assumptionSupplier, String message) throws TestAbortedException
Validate the given assumption. 'assumptionSupplier' specifies the supplier of the assumption to validate and 'message' specifies the message to be included in the TestAbortedException if the assumption is invalid.
public static void assumeFalse(boolean assumption, Supplier<String> messageSupplier) throws TestAbortedException
Validate the given assumption. 'assumption' specifies the assumption to validate and 'messageSupplier' specifies the supplier of the message to be included in the TestAbortedException if the assumption is invalid.
public static void assumeFalse(boolean assumption, String message) throws TestAbortedException
Validate the given assumption. 'assumption' specifies the assumption to validate and 'message' specifies the message to be included in the TestAbortedException if the assumption is invalid.
public static void assumeFalse(BooleanSupplier assumptionSupplier, Supplier<String> messageSupplier) throws TestAbortedException
Validate the given assumption. 'assumptionSupplier' specifies the supplier of the assumption to validate and 'messageSupplier' specifies the supplier of the message to be included in the TestAbortedException if the assumption is invalid.
public static void assumingThat(BooleanSupplier assumptionSupplier, Executable executable)
Execute the supplied Executable, but only if the supplied assumption is valid. If the assumption is invalid, this method does nothing. If the executable throws an exception, it will be rethrown as is but masked as an unchecked exception.
'assumptionSupplier' specifies the supplier of the assumption to validate and 'executable' specifies the block of code to execute if the assumption is valid.
public static void assumingThat(boolean assumption, Executable executable)
Execute the supplied Executable, but only if the supplied assumption is valid. If the assumption is invalid, this method does nothing. If the executable throws an exception, it will be rethrown as is but masked as an unchecked exception.
'assumption' specifies the assumption to validate and 'executable' specifies the block of code to execute if the assumption is valid.
Previous Next Home
No comments:
Post a Comment