我正在尝试使用ExpectedExceptions
来使用JUnit。我已经尝试了这个:
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 at java.lang.ClassLoader.defineClass1(Native Method)at java.lang.ClassLoader.defineClass(ClassLoader.java:800)at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 在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 at java.lang.ClassLoader.loadClass(ClassLoader.java:358)at org.junit.matchers.JUnitMatchers.isThrowable(JUnitMatchers.java:103) 在 org.junit.rules.ExpectedExceptionMatcherBuilder.build(ExpectedExceptionMatcherBuilder.java:27) 在 org.junit.rules.ExpectedException.handleException(ExpectedException.java:198) 在 org.junit.rules.ExpectedException.access $ 500(ExpectedException.java:85) 在 org.junit.rules.ExpectedException $ ExpectedExceptionStatement.evaluate(ExpectedException.java:177) 在org.junit.rules.RunRules.evaluate(RunRules.java:20)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)at at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 在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 at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)at at org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)at at org.junit.runners.ParentRunner.run(ParentRunner.java:309)at at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 引起: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 at java.lang.ClassLoader.loadClass(ClassLoader.java:358)... 33更多
当我这样做的时候:
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!我做错了什么?
答案 0 :(得分:3)
java.lang.NoClassDefFoundError: org/hamcrest/TypeSafeMatcher
建议你应该确保hamcrest-core在运行时类路径上,或者如果缺少它就添加它
答案 1 :(得分:2)
您需要以下依赖
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
答案 2 :(得分:0)
在应该抛出异常的测试中尝试这个:
@Test(expected = NullPointerException.class)