ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# JUnit5 假设示例 > 原文: [https://howtodoinjava.com/junit5/junit-5-assumptions-examples/](https://howtodoinjava.com/junit5/junit-5-assumptions-examples/) JUnit5 `Assumptions`类提供静态方法来支持基于假设的条件测试执行。 假设失败会导致测试中止。 [假设](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/Assumptions.html)通常在继续执行给定测试方法没有意义的情况下使用。 在测试报告中,这些测试将被标记为通过。 JUnit jupiter `Assumptions`类具有两个这样的方法:`assumeFalse()`和`assumeTrue()`。 第三个方法[`assumeThat()`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/Assumptions.html#assumingThat-boolean-org.junit.jupiter.api.function.Executable-)处于`Experimental`状态并且可能在未来得到确认。 ```java Table of Contents Assumptions.assumeTrue() Assumptions.assumeFalse() ``` ## JUnit5 的`Assumptions.assumeTrue()` `assumeTrue()`验证给定的假设为`true`,如果假设为`true`,则进行测试,否则测试执行将中止。 它具有以下重载方法。 ```java public static void assumeTrue(boolean assumption) throws TestAbortedException public static void assumeTrue(boolean assumption, Supplier<String> messageSupplier) throws TestAbortedException public static void assumeTrue(boolean assumption, String message) throws TestAbortedException public static void assumeTrue(BooleanSupplier assumptionSupplier) throws TestAbortedException public static void assumeTrue(BooleanSupplier assumptionSupplier, String message) throws TestAbortedException public static void assumeTrue(BooleanSupplier assumptionSupplier, Supplier<String> messageSupplier) throws TestAbortedException ``` ```java public class AppTest { @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 } private static String message () { return "TEST Execution Failed :: "; } } ``` ## JUnit5 的`Assumptions.assumeFalse()` `assumeFalse()`会验证给定的假设是否为假,如果假设为假,则执行测试,否则测试执行将中止。 它的工作与`assumeTrue()`相反。 它具有以下重载方法。 ```java public static void assumeFalse(boolean assumption) throws TestAbortedException public static void assumeFalse(boolean assumption, Supplier<String> messageSupplier) throws TestAbortedException public static void assumeFalse(boolean assumption, String message) throws TestAbortedException public static void assumeFalse(BooleanSupplier assumptionSupplier) throws TestAbortedException public static void assumeFalse(BooleanSupplier assumptionSupplier, String message) throws TestAbortedException public static void assumeFalse(BooleanSupplier assumptionSupplier, Supplier<String> messageSupplier) throws TestAbortedException ``` ```java public class AppTest { @Test void testOnDev() { System.setProperty("ENV", "DEV"); Assumptions.assumeFalse("DEV".equals(System.getProperty("ENV")), AppTest::message); //remainder of test will be aborted } @Test void testOnProd() { System.setProperty("ENV", "PROD"); Assumptions.assumeFalse("DEV".equals(System.getProperty("ENV"))); //remainder of test will proceed } private static String message () { return "TEST Execution Failed :: "; } } ``` 将我的问题放在评论部分。 学习愉快!