6

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?

Rafael T
  • 15,401
  • 15
  • 83
  • 144
  • Duplicate of http://stackoverflow.com/questions/156503/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests – sakura Apr 10 '14 at 13:25

3 Answers3

3

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

Morfic
  • 15,178
  • 3
  • 51
  • 61
  • 1
    I finally found it out myself: it was a Wrong Version of org.hamcrest, because of different Maven dependencys from Mockito and JUnit. – Rafael T Apr 10 '14 at 13:59
  • Aye, they both depend on hamcrest. Maybe you should post this as an answer and choose it as the correct one?! – Morfic Apr 10 '14 at 14:50
  • Also, makes sure you're running one of the JUnit releases that have removed the built-in Hamcrest JAR. – David Harkness Apr 11 '14 at 03:19
2

You need following dependency

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
daydreamer
  • 87,243
  • 191
  • 450
  • 722
0

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

@Test(expected = NullPointerException.class)
robertoia
  • 2,301
  • 23
  • 29
  • I know about Expected Parameter... but I found the class ÈxpectedException` and want to use that! http://grepcode.com/file/repo1.maven.org/maven2/junit/junit/4.8.1/org/junit/rules/ExpectedException.java – Rafael T Apr 10 '14 at 13:29
  • @RafaelT the link I added for duplication reason have all the details you require. – sakura Apr 10 '14 at 13:31
  • @sakura I saw that. But my question was really clear. I have done exactly what was suggested (even in the class or in the Answers) but it DOESN'T work! – Rafael T Apr 10 '14 at 13:33