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

Unit testing 即使指定了预期的异常,TestNG测试也会失败,unit-testing,testng,Unit Testing,Testng,我正在用TestNG框架编写测试。当我检查服务失败并且期望异常类时,除了一个测试之外,一切都正常 这是我的密码: @Test(expectedExceptions = BookNotFoundException.class) public void findBookFailureTest() throws BookNotFoundException{ when(booksRepository.findOne(new Long(1))).thenReturn(book);

我正在用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
为什么即使我指定我预期会出现异常,测试也会失败

堆栈跟踪:

-------------------------------------------------------
 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)
-------------------------------------------------------
T T S T S
-------------------------------------------------------
运行测试套件
使用org.apache.maven.surefire.TestNG.conf配置TestNG。TestNG652Configurator@5d22bbb7

测试运行:7次,失败:1次,错误:0次,跳过:0次,所用时间:0.734秒Ok问题已解决。我必须告诉我的模拟对象,在某些情况下,它必须抛出异常

以下是我的服务方法代码:

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

谢谢你的回答。这对我帮助很大

查看详细的输出,您就会知道。我们可以尝试猜测,但不能不看到代码。好的,添加了堆栈跟踪。消息非常清楚:测试应该抛出BookNotFoundException。所以它没有。所以您的代码中有一个bug。可能是您误解了
@Test(expectedExceptions=BookNotFoundException.class)
的含义吗?这并不意味着测试方法可能抛出异常;这意味着您正在断言该方法肯定会抛出异常。如果它不抛出异常,它就会失败。捕获异常是非常糟糕的做法。如果您的DAO有一个bug(例如一个NullPointerException),服务将不发送bug信号,而是隐藏它并返回一个错误的结果:这本书不存在。只捕获您真正想要处理的异常。只有当存储库返回null时才应抛出BookNotFoundException,因为该书确实不存在。