即使指定了预期的异常,TestNG也会测试失败

时间:2015-12-27 14:00:34

标签: unit-testing testng

我正在使用TestNG框架编写测试。除了一个测试,当我检查服务失败并且我期待异常类时,一切正常。

这是我的代码:

@Test(expectedExceptions = BookNotFoundException.class)
    public void findBookFailureTest() throws BookNotFoundException{
        when(booksRepository.findOne(new Long(1))).thenReturn(book);

        booksService.findBook(new Long(67));    
    }

这是测试结果:

Results :

Failed tests:
findBookFailureTest(pl.library.services.BooksServiceImplTest): (..)

Tests run: 7, Failures: 1, Errors: 0, Skipped: 0

为什么测试失败,即使我指定我期望Exception?

堆栈追踪:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@5d22bbb7
Tests run: 7, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.734 sec <<< FAILURE!
findBookFailureTest(pl.library.services.BooksServiceImplTest)  Time elapsed: 0 sec  <<< FAILURE!
org.testng.TestException:
Method BooksServiceImplTest.findBookFailureTest()[pri:0, instance:pl.library.services.BooksServiceImplTest@3b81a1bc] should have thrown an exception of class pl.exceptions.BookNotFoundException
        at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1512)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:754)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
        at org.testng.TestRunner.privateRun(TestRunner.java:767)
        at org.testng.TestRunner.run(TestRunner.java:617)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:348)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)
        at org.testng.SuiteRunner.run(SuiteRunner.java:254)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
        at org.testng.TestNG.run(TestNG.java:1057)
        at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:77)
        at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:159)
        at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:99)
        at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:106)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

1 个答案:

答案 0 :(得分:0)

好的问题解决了。我必须告诉我的Mock对象,在某些情况下,它必须抛出异常。

这是我的BooksService方法代码:

public Book findBook(Long id) throws BookNotFoundException {
    try {
        return booksRepository.findOne(id);
    } catch (Exception e) {
        throw new BookNotFoundException();
    }
}

这是工作测试:

@Test(expectedExceptions = BookNotFoundException.class)
public void findBookFailureTest() throws BookNotFoundException{
    Long id = new Long(65);
    when(booksRepository.findOne(id)).thenThrow(Exception.class);

    booksService.findBook(id);  
}

感谢您的回答。它对我很有帮助。 :)

相关问题