为了账号安全,请及时绑定邮箱和手机立即绑定

Junit5 Intellij IDEA assertsThrows() 抛出

Junit5 Intellij IDEA assertsThrows() 抛出

慕码人2483693 2021-06-08 17:01:23
我正在尝试在 Junit5 中运行测试,它应该从方法中捕获异常。它捕获 NoSuchMethodError 而不是异常异常。方法:public void thisMethodShouldThrowException() throws IllegalArgumentException {     throw new IllegalArgumentException();}测试方法:@Testvoid 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。我不知道我做错了什么。你能帮助我吗?
查看完整描述

2 回答

?
慕姐8265434

TA贡献1813条经验 获得超2个赞

我在使用时遇到了同样的错误junit-jupiter-api 5.3.2。一项快速研究表明:


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

  import org.junit.jupiter.api.Test;

  import org.junit.jupiter.api.function.Executable;

  import org.junit.jupiter.api.function.ThrowingSupplier;


  @Test

  public void testVoidMethod() {

    // works

    Executable supplier = () -> crash();

    assertThrows(NullPointerException.class, supplier);

  }


  @Test

  public void testMethodWithReturnTypeCalledByExecutable() {

    // works

    Executable supplier = () -> wrong();

    assertThrows(NullPointerException.class, supplier);

  }


  @Test

  public void testMethodWithReturnType() {

    // fails

    assertThrows(NullPointerException.class, () -> wrong());

  }


  @Test

  public void testMethodWithReturnTypeCalledByThrowingSupplier() {

    // fails

    ThrowingSupplier<?> supplier = () -> wrong();

    assertThrows(NullPointerException.class, supplier);

  }


  public void crash() {

    throw new NullPointerException();

  }


  public Object wrong() {

    throw new NullPointerException();

  }

使用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。


查看完整回答
反对 回复 2021-06-23
?
哆啦的时光机

TA贡献1779条经验 获得超6个赞

我正在用我正在编码的东西查找同样的问题,我通过将测试方法放入大括号中来解决我的问题,如下所示:


Assertions.assertThrows(IllegalArgumentException.class,

        () -> {dbProperties.thisMethodShouldThrowException();} );


查看完整回答
反对 回复 2021-06-23
  • 2 回答
  • 0 关注
  • 457 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信