TestNG expectedException未找到抛出的异常

时间:2015-07-14 21:10:57

标签: java spring testng

我有一个类,用于映射从json对象通过REST传递的值。前端javascript正在传递所有传入的值字符串。如果我只需要字符串值,使用Spring和Jackson,一切正常。但是,我有一个字段,我需要转换为int。由于可以提交一个不可解析的字符串,我在try / catch块中得到它应该抛出FormatNumberException。我有一个测试方法,应该测试以确保抛出此错误。但是,它没有抓住它,它正在抛出这个异常,但测试仍然失败。非常感谢任何帮助。

这是MyClass中方法的代码:     public void setMaxNumOfFiles(String value)//抛出NumberFormatException     {

    try
    {
        this.maxNumOfFiles = Integer.parseInt(value);
    }
    catch (NumberFormatException e)
    {
        e.printStackTrace();
    }
}

我在最后一次尝试时已经注意到了投掷部分 这是我测试的代码:     public void setMaxNumOfFiles(String value)//抛出NumberFormatException     {

    try
    {
        this.maxNumOfFiles = Integer.parseInt(value);
    }
    catch (NumberFormatException e)
    {
        e.printStackTrace();
    }
}

我觉得很简单。下面是我运行测试时给我的控制台:

{
    java.lang.NumberFormatException: For input string: "foo"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
...
PASSED: setMaxNumOfFilesShouldSetToAndIntegerIfPassedString
FAILED: setMaxNumOfFilesShouldThrowExceptionIfPassedInUnparseableString
org.testng.TestException: 
Method      HeaderQueryTest.setMaxNumOfFilesShouldThrowExceptionIfPassedInUnparseableString()[pri:0, instance:test.visa.casanalytics.model.HeaderQueryTest@47f37ef1] should have thrown an exception of type class java.lang.NumberFormatException
at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1405)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:690)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:834)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1142)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:771)
at org.testng.TestRunner.run(TestRunner.java:621)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1176)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1101)
at org.testng.TestNG.run(TestNG.java:1009)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)


===============================================
    Default test
    Tests run: 35, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 35, Failures: 1, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.EmailableReporter2@77afea7d: 22 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@3d04a311: 25 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@299a06ac: 262 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 22 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@5f8ed237: 12 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@2e5c649: 83 ms

}

1 个答案:

答案 0 :(得分:0)

摆脱捕获并尝试抛出异常,向测试方法添加抛出。在expectedExceptions中添加异常。您的测试方法将在收到预期的异常后通过