【问题标题】:How to work with junit ExpectedException?如何使用junit ExpectedException?
【发布时间】:2014-05-24 05:37:28
【问题描述】:

我正在尝试为 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(本机方法)在 java.lang.ClassLoader.defineClass(ClassLoader.java:800) 在 java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 在 java.net.URLClassLoader.defineClass(URLClassLoader.java:449) 在 java.net.URLClassLoader.access$100(URLClassLoader.java:71) 在 java.net.URLClassLoader$1.run(URLClassLoader.java:361) 在 java.net.URLClassLoader$1.run(URLClassLoader.java:355) 在 java.security.AccessController.doPrivileged(Native Method) 在 java.net.URLClassLoader.findClass(URLClassLoader.java:354) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:425) 在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:358) 在 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) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 在 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) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:309) 在 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 在 java.net.URLClassLoader$1.run(URLClassLoader.java:366) 在 java.net.URLClassLoader$1.run(URLClassLoader.java:355) 在 java.security.AccessController.doPrivileged(Native Method) 在 java.net.URLClassLoader.findClass(URLClassLoader.java:354) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:425) 在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 在 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!我做错了什么?

【问题讨论】:

标签: java unit-testing junit4 hamcrest expected-exception


【解决方案1】:

你需要以下依赖

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我终于自己发现了:这是 org.hamcrest 的错误版本,因为 Mockito 和 JUnit 的 Maven 依赖项不同。
    • 是的,它们都依赖于 hamcrest。也许您应该将此作为答案发布并选择正确的答案?!
    • 另外,请确保您正在运行已删除内置 Hamcrest JAR 的 JUnit 版本之一。
    【解决方案3】:

    在应该抛出异常的测试中试试这个:

    @Test(expected = NullPointerException.class)
    

    【讨论】:

    • 我知道 Expected Parameter... 但我找到了 ÈxpectedException` 类并想使用它! grepcode.com/file/repo1.maven.org/maven2/junit/junit/4.8.1/org/…
    • @RafaelT 我出于重复原因添加的链接包含您需要的所有详细信息。
    • @sakura 我看到了。但我的问题很清楚。我已经完全按照建议完成了(即使在课堂上或答案中),但它不起作用!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多