Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException when toString() of parameter returns null #66

Closed
PascalSchumacher opened this issue Dec 13, 2015 · 4 comments
Closed

Comments

@PascalSchumacher
Copy link
Contributor

Hi,

thanks for providing junit-dataprovider.

While working on a pull request to switch assertj from JUnitParams to junit-dataprovider I encountered the this problem.

When I try to use null as a parameter, e.g.:

  @DataProvider()
  public static Object[][] testDescriptionGenerator() {
    return new Object[][] {
        { new TestDescription(null) }
  }

  @Test
  @UseDataProvider("testDescriptionGenerator")
  public void should_return_empty_String(TestDescription testDescription) {
    assertThat(formatter.format(testDescription)).isEmpty();
  }

I get

java.lang.NullPointerException
    at com.tngtech.java.junit.dataprovider.internal.placeholder.ParameterPlaceholder.format(ParameterPlaceholder.java:99)
    at com.tngtech.java.junit.dataprovider.internal.placeholder.ParameterPlaceholder.formatAll(ParameterPlaceholder.java:70)
    at com.tngtech.java.junit.dataprovider.internal.placeholder.ParameterPlaceholder.getReplacementFor(ParameterPlaceholder.java:57)
    at com.tngtech.java.junit.dataprovider.internal.placeholder.BasePlaceholder.process(BasePlaceholder.java:61)
    at com.tngtech.java.junit.dataprovider.DataProviderFrameworkMethod.getName(DataProviderFrameworkMethod.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.testName(BlockJUnit4ClassRunner.java:225)
    at org.junit.runners.BlockJUnit4ClassRunner.describeChild(BlockJUnit4ClassRunner.java:96)
    at org.junit.runners.BlockJUnit4ClassRunner.describeChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.getDescription(ParentRunner.java:352)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:85)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Because in this case TestDescription#toString returns null. Not sure if this is something that junit-dataprovider should handle?

@PascalSchumacher PascalSchumacher changed the title NullPointerException when using null as value of object parameter NullPointerException when toString() of parameter returns null Dec 13, 2015
@aaschmid aaschmid self-assigned this Dec 13, 2015
@aaschmid
Copy link
Member

Hi @PascalSchumacher, the problem is really that TestDescription#toString() returns null as the junit-dataprovider ParameterPlaceholder line 93 to 99 are

        String result;
        if (param instanceof String) {
            result = (String) param;
        } else {
            result = param.toString();
        }
        return result.replaceAll("\\r", "\\\\r").replaceAll("\\n", "\\\\n");

I will fix it in a moment :-)
Thanks for reporting this!

@aaschmid aaschmid added this to the next version milestone Dec 13, 2015
@aaschmid
Copy link
Member

Is it somehow urgent? I can provide you a snapshot version to retest if you want. Otherwise I can create a new release hopefully before christmas. What would you prefer @PascalSchumacher?

aaschmid added a commit that referenced this issue Dec 13, 2015
* seems that findbugs assumes that toString() never returns null :-S
@aaschmid
Copy link
Member

Btw. second commit suppresses findbugs warning because findbugs assumes that toString() does not return null :-S

@PascalSchumacher
Copy link
Contributor Author

Thanks for the ultra quick fix. 👍

Yes, returning null from toString() is not a good practice.

Is it somehow urgent? I can provide you a snapshot version to retest if you want. Otherwise I can >create a new release hopefully before christmas. What would you prefer @PascalSchumacher?

Thanks for the offer, but it's not urgent so I'm fine with waiting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants