java 无法解析“Assertions”中的方法“assertNotThrows”

soat7uwm  于 2023-05-12  发布在  Java
关注(0)|答案(2)|浏览(262)

下面是JUnit4代码:

Assertions.assertNotThrows(ExceptionNotToThrow.class, () -> {
  .....
});

我试着把这段代码迁移到Junit5上:

Assertions.assertDoesNotThrow(ExceptionNotToThrow.class, () -> {
  .....
});

但我得到了:

Cannot resolve method 'assertDoesNotThrow(Class<ExceptionNotToThrow>, <lambda expression>)'

你知道我怎样才能正确地迁移这些代码吗?

fjnneemd

fjnneemd1#

你可以试试这个:

try {
    .....
} catch (ExceptionNotToThrow e) {
    Assertions.fail(e)
} catch (Exception e) {
    // this is expected
}
dphi5xsq

dphi5xsq2#

JUnit 5的内置Assertions类没有接受预期异常类型的assertDoesNotThrow方法重载。在我看来,Assert一个特定的异常不会被抛出是没有意义的。为什么某个异常会导致测试失败,而如果抛出其他异常,测试就应该通过?如果你期望抛出 something,那么我认为assertThrows是更合适的Assert。
无论如何,您可以执行以下操作来模拟JUnit 4代码的行为(尽管我在JUnit 4的the documentation中找不到这样的assertNotThrows方法?).

package sample;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.fail;

class Tests {

    @Test
    void test() {
        try {
            // call method that can throw exceptions
        } catch (ExceptionNotToThrow ex) {
            fail("expected exception not to be thrown (see cause)", ex);
        } catch (Exception ex) {
            // this is expected; do nothing
        }
    }

    // this is here to allow the code to compile
    static class ExceptionNotToThrow extends RuntimeException {}
}

但是如果您需要多次这样做,那么我建议您为此创建自己的Assert方法。例如:

package sample;

import java.util.function.Supplier;
import org.junit.jupiter.api.function.Executable;

// this API became stable in 5.9
import static org.junit.jupiter.api.AssertionFailureBuilder.assertionFailure;;

public class MyAssertions {

    public static void assertDoesNotThrow(Class<? extends Throwable> type, Executable exec) {
        assertDoesNotThrow(type, exec, (Object) null);
    }

    public static void assertDoesNotThrow(
            Class<? extends Throwable> type, Executable exec, String message) {
        assertDoesNotThrow(type, exec, (Object) message);
    }

    public static void assertDoesNotThrow(
            Class<? extends Throwable> type, Executable exec, Supplier<String> messageSupplier) {
        assertDoesNotThrow(type, exec, (Object) messageSupplier);
    }
    
    private static void assertDoesNotThrow(
            Class<? extends Throwable> type, Executable exec, Object messageOrSupplier) {
        try {
            exec.execute();
        } catch (Throwable t) {
            if (type.isInstance(t)) {
                assertionFailure()
                    .message(messageOrSupplier)
                    .reason("Exception thrown when it should not have been: " + t.getClass().getName())
                    .cause(t)
                    .buildAndThrow();
            }
        }
    }
}

它可以用于:

package sample;

import org.junit.jupiter.api.Test;

import static sample.MyAssertions.assertDoesNotThrow;

class Tests {

    @Test
    void test() {
        assertDoesNotThrow(ExceptionNotToThrow.class, () -> { /* ... */ });
    }

    // this is here to allow the code to compile
    static class ExceptionNotToThrow extends RuntimeException {}
}

或者,您可以查看是否存在已经提供类似上述内容的第三方库。

相关问题