我正在尝试在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());
}
build.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>
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版本为2018.2 我也尝试了最新版本的Junit5。 我不知道我在做什么错。你能帮我吗?
答案 0 :(得分:1)
我在使用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
作为参数的方法ThrowingSupplier
。仅剩下那些Executable
的人。但是有些新方法采用名为ThrowingSupplier
的{{1}}。
最后,我更新为assertDoesNotThrow
。
5.4 Release Notes提到了类似情况的错误修正。但这不是junit-jupiter-api 5.5.2
的解释。
更新:
如果我通过Eclipse的JUnit插件运行JUnit测试,则会抛出NoSuchMethodError
。它使用NoSuchMethodError
,而在org.junit.jupiter.api_5.1.0.v20180327-1502.jar
中指定了JUnit 5.3.2。在JUnit 5.3.2上使用maven成功运行测试。
这是我使用Eclipse的情况。我没有IntelliJ IDEA。
答案 1 :(得分:0)
我正在用我正在编写的代码查找相同的问题,并且通过将测试方法放在大括号中来解决了这个问题,
Assertions.assertThrows(IllegalArgumentException.class,
() -> {dbProperties.thisMethodShouldThrowException();} );