TNG / junit-dataprovider

A TestNG like dataprovider runner for JUnit with many additional features
Apache License 2.0
245 stars 164 forks source link

Confusing Error/NPE when @DataProvider method is non-static #45

Closed seanf closed 9 years ago

seanf commented 9 years ago

Adapting a former TestNG test, I had a data provider method which was not static, and got this error message. It took me a while to work out what was wrong, so a clearer error message would be nice to have.

java.lang.Error: Cannot explode 'ResourceUtilsTest.encodeDocIds' using 'urlPatterns' due to: Exception while invoking dataprovider method 'urlPatterns': null
    at com.tngtech.java.junit.dataprovider.internal.TestGenerator.generateExplodedTestMethodsFor(TestGenerator.java:46)
    at com.tngtech.java.junit.dataprovider.DataProviderRunner.generateExplodedTestMethodsFor(DataProviderRunner.java:241)
    at com.tngtech.java.junit.dataprovider.DataProviderRunner.computeTestMethods(DataProviderRunner.java:172)
    at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:168)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:104)
    at com.tngtech.java.junit.dataprovider.DataProviderRunner.collectInitializationErrors(DataProviderRunner.java:91)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:355)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:57)
    at com.tngtech.java.junit.dataprovider.DataProviderRunner.<init>(DataProviderRunner.java:80)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:41)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.IllegalArgumentException: Exception while invoking dataprovider method 'urlPatterns': null
    at com.tngtech.java.junit.dataprovider.internal.TestGenerator.explodeTestMethod(TestGenerator.java:84)
    at com.tngtech.java.junit.dataprovider.internal.TestGenerator.generateExplodedTestMethodsFor(TestGenerator.java:44)
    ... 27 more
Caused by: java.lang.NullPointerException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at com.tngtech.java.junit.dataprovider.internal.TestGenerator.explodeTestMethod(TestGenerator.java:81)
    ... 28 more

(I'm using version 1.9.3.)

aaschmid commented 9 years ago

Hi @seanf, thanks for your issue. Do you have a formulation in mind which would have help you better? Thanks & Regards, Andreas

seanf commented 9 years ago

How about throwing an exception in TestGenerator.explodeTestMethod() with a message like "DataProvider methods must not be static" when dataProviderMethod has Modifier.STATIC?

On 29 May 2015 at 17:32, Andreas Schmid notifications@github.com wrote:

Hi @seanf https://github.com/seanf, thanks for your issue. Do you have a formulation in mind which would have help you better? Thanks & Regards, Andreas

— Reply to this email directly or view it on GitHub https://github.com/TNG/junit-dataprovider/issues/45#issuecomment-106725769 .

aaschmid commented 9 years ago

I will fix it during the weekend. Thanks!

seanf commented 9 years ago

Thanks for a great tool!

On 29 May 2015 at 17:45, Andreas Schmid notifications@github.com wrote:

I will fix it during the weekend. Thanks!

— Reply to this email directly or view it on GitHub https://github.com/TNG/junit-dataprovider/issues/45#issuecomment-106730294 .

aaschmid commented 9 years ago

Thanks for reporting!