猿问

Junit5 Intellij IDEA assertsThrows() 抛出

我正在尝试在 Junit5 中运行测试,它应该从方法中捕获异常。它捕获 NoSuchMethodError 而不是异常异常。


方法:


public void thisMethodShouldThrowException() throws IllegalArgumentException {

     throw new IllegalArgumentException();

}

测试方法:


@Test

void thisMethodShouldThrowException() throws IllegalArgumentException {

    DBProperties dbProperties = DBProperties.getInstance();

    Assertions.assertThrows(IllegalArgumentException.class,

        ()->dbProperties.thisMethodShouldThrowException());

}

构建.gradle:


dependencies {

    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.3.0-alpha4'

    compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.11'

    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0'

    testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.0.0'

    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0'

    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.0.0'

    testCompile("org.junit.vintage:junit-vintage-engine:4.12.0")

}

堆栈跟踪:


org.opentest4j.AssertionFailedError: Unexpected exception type thrown ==> 

Expected :<java.lang.IllegalArgumentException> 

Actual   :<java.lang.NoSuchMethodError>

<Click to see difference>


Intellij Idea 版本是 2018.2 我也尝试了最新版本的 Junit5。我不知道我做错了什么。你能帮助我吗?


慕码人2483693
浏览 458回答 2
2回答

慕姐8265434

我在使用时遇到了同样的错误junit-jupiter-api 5.3.2。一项快速研究表明:&nbsp; import static org.junit.jupiter.api.Assertions.assertThrows;&nbsp; import org.junit.jupiter.api.Test;&nbsp; import org.junit.jupiter.api.function.Executable;&nbsp; import org.junit.jupiter.api.function.ThrowingSupplier;&nbsp; @Test&nbsp; public void testVoidMethod() {&nbsp; &nbsp; // works&nbsp; &nbsp; Executable supplier = () -> crash();&nbsp; &nbsp; assertThrows(NullPointerException.class, supplier);&nbsp; }&nbsp; @Test&nbsp; public void testMethodWithReturnTypeCalledByExecutable() {&nbsp; &nbsp; // works&nbsp; &nbsp; Executable supplier = () -> wrong();&nbsp; &nbsp; assertThrows(NullPointerException.class, supplier);&nbsp; }&nbsp; @Test&nbsp; public void testMethodWithReturnType() {&nbsp; &nbsp; // fails&nbsp; &nbsp; assertThrows(NullPointerException.class, () -> wrong());&nbsp; }&nbsp; @Test&nbsp; public void testMethodWithReturnTypeCalledByThrowingSupplier() {&nbsp; &nbsp; // fails&nbsp; &nbsp; ThrowingSupplier<?> supplier = () -> wrong();&nbsp; &nbsp; assertThrows(NullPointerException.class, supplier);&nbsp; }&nbsp; public void crash() {&nbsp; &nbsp; throw new NullPointerException();&nbsp; }&nbsp; public Object wrong() {&nbsp; &nbsp; throw new NullPointerException();&nbsp; }使用assertThrows带有ThrowingSupplier提出了一个NoSuchMethodError。我更新到junit-jupiter-api 5.4.0. API 发生了变化。不再assertThrows有带有ThrowingSupplieras 参数的方法。只有那些接受了Executable。但是有一些新方法采用ThrowingSupplier命名的assertDoesNotThrow.最后我更新到junit-jupiter-api 5.5.2.在5.4版本说明提到的类似病例的bug修正。然而,这不是对NoSuchMethodError.更新:NoSuchMethodError如果我通过 Eclipse 的 JUnit 插件运行 JUnit 测试,则会抛出 。它使用org.junit.jupiter.api_5.1.0.v20180327-1502.jar而在pom.xmlJUnit 5.3.2 中被指定。在 JUnit 5.3.2 上使用 maven 运行测试成功。这是我使用 Eclipse 的情况。我没有 IntelliJ IDEA。

哆啦的时光机

我正在用我正在编码的东西查找同样的问题,我通过将测试方法放入大括号中来解决我的问题,如下所示:Assertions.assertThrows(IllegalArgumentException.class,&nbsp; &nbsp; &nbsp; &nbsp; () -> {dbProperties.thisMethodShouldThrowException();} );
随时随地看视频慕课网APP

相关分类

Java
我要回答