测试用例失败时使用EasyTest的ParameterizedAssertionError

时间:2014-12-01 16:44:01

标签: java unit-testing junit4

我想尝试使用EasyTest从CSV获取输入参数,并在此处发布的博客中找到以下内容,编写得非常好:

http://gpcmol.blogspot.com/2013/06/easytest-unit-testing-with-externalized.html

如果我按照这个例子运行它会很精彩地给我一个不错的PDF输出。

但是,如果我导致测试用例失败,则通过更改",9,-12"的最后一行输入。到",9,-13",我不再获得PDF输出并获得2次失败而不是1次失败。

第一个失败是测试用例失败的正确断言。第二个失败是以下异常:

<testcase name="classMethod" classname="TransformCelciusTest" time="0.0">
    <failure message="org.junit.experimental.theories.internal.ParameterizedAssertionError: testToCelsiusConverter(TestInfo [testClass=org.junit.runners.model.TestClass@90bb3e6, dataLoader=org.easetech.easytest.loader.CSVDataLoader@5f4fc5ad, filePaths=[data/temperatureConversionData.csv], methodName=testToCelsiusConverter])" type="org.junit.experimental.theories.internal.ParameterizedAssertionError">org.junit.experimental.theories.internal.ParameterizedAssertionError: testToCelsiusConverter(TestInfo [testClass=org.junit.runners.model.TestClass@90bb3e6, dataLoader=org.easetech.easytest.loader.CSVDataLoader@5f4fc5ad, filePaths=[data/temperatureConversionData.csv], methodName=testToCelsiusConverter])
    at org.easetech.easytest.util.RunAftersWithOutputData.writeData(RunAftersWithOutputData.java:157)
    at org.easetech.easytest.util.RunAftersWithOutputData.evaluate(RunAftersWithOutputData.java:133)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
    at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
    at org.easetech.easytest.loader.CSVDataLoader.writeDataToCSV(CSVDataLoader.java:364)
    at org.easetech.easytest.loader.CSVDataLoader.writeData(CSVDataLoader.java:180)
    at org.easetech.easytest.util.RunAftersWithOutputData.writeData(RunAftersWithOutputData.java:154)
    ... 27 more
Caused by: java.lang.NullPointerException
    at org.easetech.easytest.loader.CSVDataLoader.writeOutputData(CSVDataLoader.java:382)
    at org.easetech.easytest.loader.CSVDataLoader.writeDataToCSV(CSVDataLoader.java:347)
    ... 29 more
</failure>
  </testcase>

任何人都了解如何修改示例以便在没有ParameterizedAssertionError异常的情况下正确完成,以便可以在输出中正确报告测试用例失败(PDF)?

我认为这是相关的: JUnit @Theory : is there a way to throw meaningful exception?

1 个答案:

答案 0 :(得分:0)

我使用EasyTest Core 1.3.1库测试了该场景,实际上它是EasyTest 1.3.1中的一个问题(实际上是一个错误)。具体来说,CSVDataLoader中存在NullPointerException,因为它期望测试持续时间值,但由于测试失败而不存在。我必须看到这个问题的最佳解决方案。我会告诉你解决方案。与此同时,您可以尝试使用Excel和XML数据加载器。或者,如果您想要快速解决方案,可以复制粘贴CSVDataLoader并覆盖CSVDataLoader的第364行,以便检查DURATIOn是否存在,然后再调用toString。然后,您可以在@DataLoader注释中将此Loader用作自定义加载程序。

谢谢, Anuj Kumar