简体   繁体   English

Junit5 Intellij IDEA assertsThrows() 抛出 noSuchMethodError

[英]Junit5 Intellij IDEA assertsThrows() throws noSuchMethodError

I'm trying to run test in Junit5 which should catch an exception from method.我正在尝试在 Junit5 中运行测试,它应该从方法中捕获异常。 It catches NoSuchMethodError instead of excepted exception.它捕获 NoSuchMethodError 而不是异常异常。

Method:方法:

public void thisMethodShouldThrowException() throws IllegalArgumentException {
     throw new IllegalArgumentException();
}

Test method:测试方法:

@Test
void thisMethodShouldThrowException() throws IllegalArgumentException {
    DBProperties dbProperties = DBProperties.getInstance();
    Assertions.assertThrows(IllegalArgumentException.class,
        ()->dbProperties.thisMethodShouldThrowException());
}

build.gradle:构建.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")
}

Stack trace:堆栈跟踪:

org.opentest4j.AssertionFailedError: Unexpected exception type thrown ==> 
Expected :<java.lang.IllegalArgumentException> 
Actual   :<java.lang.NoSuchMethodError>
<Click to see difference>


at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:59)
at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:38)
at org.junit.jupiter.api.Assertions.assertThrows(Assertions.java:1108)
at pl.javamill.bnbahistory.Controller.DBPropertiesTest.thisMethodShouldThrowException(DBPropertiesTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:389)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:167)
at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:163)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:110)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:83)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null$2(HierarchicalTestExecutor.java:92)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:92)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null$2(HierarchicalTestExecutor.java:92)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:92)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:51)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.NoSuchMethodError: pl.javamill.bnbahistory.Controller.DBProperties.thisMethodShouldThrowException()V
at pl.javamill.bnbahistory.Controller.DBPropertiesTest.lambda$thisMethodShouldThrowException$1(DBPropertiesTest.java:24)
at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:50)
... 54 more

Intellij Idea version is 2018.2 I tried lastest version of Junit5 also. Intellij Idea 版本是 2018.2 我也尝试了最新版本的 Junit5。 I have no idea what I'm doing wrong.我不知道我做错了什么。 Can you help me?你能帮助我吗?

I encountered the same error using junit-jupiter-api 5.3.2 .我在使用junit-jupiter-api 5.3.2遇到了同样的错误。 A quick research showed the following:一项快速研究表明:

  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();
  }

Using assertThrows with a ThrowingSupplier raises a NoSuchMethodError .assertThrowsThrowingSupplier一起使用会引发NoSuchMethodError

I update to junit-jupiter-api 5.4.0 .我更新到junit-jupiter-api 5.4.0 The API changed. API 发生了变化。 There is no method assertThrows with a ThrowingSupplier as parameter anymore.不再有以ThrowingSupplier作为参数的assertThrows方法。 Only the ones taking an Executable remained.只有那些接受Executable留下了。 But there are some new methods taking a ThrowingSupplier named assertDoesNotThrow .但是有一些新方法采用名为assertDoesNotThrowThrowingSupplier

Finally I updated to junit-jupiter-api 5.5.2 .最后我更新到junit-jupiter-api 5.5.2

The 5.4 Release Notes mention a bugfix of a similar case. 5.4 发行说明提到了类似案例的错误修复。 However this is not an explanation for the NoSuchMethodError .然而,这不是对NoSuchMethodError的解释。

UPDATE:更新:

The NoSuchMethodError is thrown if I run the JUnit tests via Eclipse's JUnit plugin.如果我通过 Eclipse 的 JUnit 插件运行 JUnit 测试,则会抛出NoSuchMethodError It uses org.junit.jupiter.api_5.1.0.v20180327-1502.jar whereas in the pom.xml JUnit 5.3.2 was specified.它使用org.junit.jupiter.api_5.1.0.v20180327-1502.jar而在pom.xml中指定了 JUnit 5.3.2。 Running the tests with maven at JUnit 5.3.2 succeeds.在 JUnit 5.3.2 上使用 maven 运行测试成功。

This was my case using Eclipse.这是我使用 Eclipse 的情况。 I don't have IntelliJ IDEA.我没有 IntelliJ IDEA。

I was looking up this same problem with something I was coding, and I fixed my issue by putting the test method into curly braces like so:我正在用我正在编码的东西查找同样的问题,我通过将测试方法放入大括号中来解决我的问题,如下所示:

Assertions.assertThrows(IllegalArgumentException.class,
        () -> {dbProperties.thisMethodShouldThrowException();} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM