gpt4 book ai didi

java - 如何使用 junit ExpectedException?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:34 25 4
gpt4 key购买 nike

我正在尝试使用 JUnit 的 ExpectedExceptions。我已经试过了:

public class ExpectedTest {

@Rule
public ExpectedException thrown = ExpectedException.none();


@Test
public void test() {
thrown.expect(NullPointerException.class);
throw new NullPointerException();
}

}

这引发了以下异常:

java.lang.NoClassDefFoundError: org/hamcrest/TypeSafeMatcher at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:800) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at org.junit.matchers.JUnitMatchers.isThrowable(JUnitMatchers.java:103) at org.junit.rules.ExpectedExceptionMatcherBuilder.build(ExpectedExceptionMatcherBuilder.java:27) at org.junit.rules.ExpectedException.handleException(ExpectedException.java:198) at org.junit.rules.ExpectedException.access$500(ExpectedException.java:85) at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:177) at org.junit.rules.RunRules.evaluate(RunRules.java:20) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: java.lang.ClassNotFoundException: org.hamcrest.TypeSafeMatcher at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) ... 33 more

当我这样做时:

public class ExpectedTest {

@Rule
public ExpectedException thrown;

@Before
public void setup() {
thrown = ExpectedException.none();
}

@Test
public void test() {
thrown.expect(NullPointerException.class);
throw new NullPointerException();
}

}

我得到一个简单的 NullPointerException!我做错了什么?

最佳答案

java.lang.NoClassDefFoundError: org/hamcrest/TypeSafeMatcher 建议您确保 hamcrest-core 在运行时类路径中,如果缺少则添加它

关于java - 如何使用 junit ExpectedException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22989302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com