6
votes

I'm trying to work with ExpectedExceptions for JUnit. I tried already this:

public class ExpectedTest {

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


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

}

which raises me the following Exception:

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

When I do it like this:

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

}

I get a simple NullPointerException! What am I doing wrong?

3

3 Answers

3
votes

java.lang.NoClassDefFoundError: org/hamcrest/TypeSafeMatcher suggests that you should make sure that hamcrest-core is on the runtime classpath, or add it if it's missing

2
votes

You need following dependency

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

Try this in the test that is supposed to throw the exception:

@Test(expected = NullPointerException.class)