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

Problems with test method parameters of type String containing newline #41

Closed
g-fresh opened this issue Sep 30, 2014 · 4 comments
Closed

Comments

@g-fresh
Copy link

g-fresh commented Sep 30, 2014

In version 1.9.2, using data a provider which produces strings containing newline, results in strange behavior.

Exampe:

import com.tngtech.java.junit.dataprovider.*;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(DataProviderRunner.class)
public class MyTest {

    @DataProvider
    public static Object[][] blankInput() {
        return new Object[][] {
                {""}, {" "}, {"\t"}, {"\n"}
        };
    }

    @Test(expected = IllegalArgumentException.class)
    @UseDataProvider("blankInput")
    public void throwsExceptionOnBlankString(String blankString) throws Exception {
        if (blankString.trim().length() == 0) {
            throw new IllegalArgumentException(blankString);
        }
    }
}

Running the method separately (not the complete test case), you get:

java.lang.IllegalArgumentException: Test method description Method throwsExceptionOnBlankString(sample.MyTest) is not suppored by DataProviderFilter.
    at com.tngtech.java.junit.dataprovider.DataProviderFilter.shouldRun(DataProviderFilter.java:59)
    at org.junit.runners.ParentRunner.shouldRun(ParentRunner.java:373)
    at org.junit.runners.ParentRunner.filter(ParentRunner.java:326)
    at com.tngtech.java.junit.dataprovider.DataProviderRunner.filter(DataProviderRunner.java:42)
    at org.junit.runner.manipulation.Filter.apply(Filter.java:97)
    at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:32)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestMethodReference.<init>(JUnit4TestMethodReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:54)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Without the last parameter ("\n"), things work as expected.

Running the complete test case from the console, everythings works as expected.
Running the complete test case from Eclipse, you don't get any test results, meaning:
The JUnit view shows the list of tests, but reports 0/4 runs.

@janschaefer
Copy link

How do you execute the test? Which Java and which JUnit version do you use? I cannot reproduce the problem. It works for me in Eclipse and the gradle test runner with Java 1.7 and JUnit 4.8.2.

@aaschmid aaschmid added this to the next version milestone Sep 30, 2014
@aaschmid aaschmid self-assigned this Sep 30, 2014
@aaschmid
Copy link
Member

Hi @g-fresh, hi @janschaefer,

I cannot reproduce the Exception in Eclipse Luna as well but I have the problem, that the JUnit plugin does not show the test result properly due to the newline in the last tests String.

What I can imagine to avoid this is adding a special formatting for \t, \n, \r (as "" prints out <empty string> instead of :-)

Using ./gradlew build having the above code integrated as test succeed as well and outputs readable test results.

@aaschmid
Copy link
Member

aaschmid commented Oct 8, 2014

I just reconsidered may former comment about IntelliJ IDEA and saw that I made a mistake. Actually there is no problem with IntelliJ and newlines character in test method descriptions (=> delete the former post with the stacktrace ...)

@aaschmid
Copy link
Member

aaschmid commented Oct 8, 2014

Hi @g-fresh,

I just tried to reproduce your bug again but still no sucess.
In your stacktrace above the IllegalArgumentException is thrown in DataProviderFilter. This means to me that you do not use the v1.9.2 of JUnit dataprovider as since v1.8.0 I have made DataProviderFilter more failure tolerant such that it no longer throws any IllegalArgumentException (see #27).
Moreover, if I look at any version between v1.4.0 and v1.5.2 the line 59f looks like this:

throw new IllegalArgumentException(String.format("Test method description %s is not suppored by %s.",
        filter.describe(), this.getClass().getSimpleName()));

Furthermore, in every version between v1.4.0 and v1.7.0 I can reproduce your reported bug for the included test containing the newline character. It is thrown because the filter description does not match the corresponding pattern (which is rather logical as it contains a newline and the matcher does not recognize newlines with .. To fix this one could just add the Pattern.DOTALL flag to the pattern.

With #42 and the replacement of newlines with their printable companion, this would be no longer necessary to fix, but I think it is worth to fix.
What do others think?

aaschmid added a commit that referenced this issue Jan 24, 2015
* master:
  prepared next development version, v1.9.4-SNAPSHOT
  prepared release for v1.9.3
  fixed findbugsMain task for Java < v1.7
  added test for JDK 8 by travis.ci
  improvements of build.gradle and javadoc
  fixed filter pattern to also match newlines (using Pattern.DOTALL) (#41)
  Class-Name in generous pattern must have only valid java identifier
  added acceptance test to use newlines with dataprovider
  added defaults of @dataProvider properties to javadoc
  fixed #42 (JUnit cannot deal with newlines in test method parameters)
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

3 participants