How to work with junit ExpectedException?
Asked Answered
G

3

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?

Glenoid answered 10/4, 2014 at 13:21 Comment(1)
Duplicate of #157003Ceballos
C
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

Camshaft answered 10/4, 2014 at 13:40 Comment(3)
I finally found it out myself: it was a Wrong Version of org.hamcrest, because of different Maven dependencys from Mockito and JUnit.Glenoid
Aye, they both depend on hamcrest. Maybe you should post this as an answer and choose it as the correct one?!Camshaft
Also, makes sure you're running one of the JUnit releases that have removed the built-in Hamcrest JAR.Robinett
B
2

You need following dependency

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
Benton answered 28/10, 2014 at 12:21 Comment(0)
W
0

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

@Test(expected = NullPointerException.class)
Wuhan answered 10/4, 2014 at 13:22 Comment(3)
I know about Expected Parameter... but I found the class ÈxpectedException` and want to use that! grepcode.com/file/repo1.maven.org/maven2/junit/junit/4.8.1/org/…Glenoid
@RafaelT the link I added for duplication reason have all the details you require.Ceballos
@Ceballos 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!Glenoid

© 2022 - 2024 — McMap. All rights reserved.