问题
I'm running unit test on the following class:
public class FileClass {
public void funcB() {
try {
throw new ClassNotFoundException();
}
catch( ClassNotFoundException e ) {
}
}
}
Using Mockito code as shown below:
public class TestFileClass {
@Test(expected=ClassNotFoundException.class)
public void testFuncB() {
FileClass fc = Mockito.spy(new FileClass());
fc.funcB();
}
}
But the test were failed due to following error:
java.lang.AssertionError: Expected exception: java.lang.ClassNotFoundException
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:35)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
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)
I did also try on PowerMockito but failed again. Any clue with this error? It seems to me that I can't run any test on ClassNotFoundException
?
回答1:
The method funcB() should throw the exception, not catch it. The code bellow and test work fine:
public class FileClass {
public void funcB() throws ClassNotFoundException {
//try {
throw new ClassNotFoundException();
//} catch ( ClassNotFoundException e) {
//}
}
}
Mockito test:
public class TestFileClass {
@Test(expected = ClassNotFoundException.class)
public void testFuncB() throws ClassNotFoundException {
FileClass fc = Mockito.spy(new FileClass());
fc.funcB();
}
}
回答2:
funcB is not throwing the exception because you are catching it within funcB's body.
回答3:
I think that describe way of testing exception (expected annotation in @Test) is not the most elegant solution - it break given/when/then convention
The best choice is catch-exception library
http://code.google.com/p/catch-exception/
回答4:
catch-exception in nice. But the assertj (http://joel-costigliola.github.io/assertj/) has much better support for exception checking
assertThatThrownBy(new DummyService()::someMethod)
.isInstanceOf(RuntimeException.class)
.hasMessage("Runtime exception occurred")
.hasNoCause();
来源:https://stackoverflow.com/questions/20986195/how-could-i-test-classnotfoundexception-using-mockito