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

Filters doesn't work #6

Closed
Lucas3oo opened this issue Aug 9, 2014 · 3 comments
Closed

Filters doesn't work #6

Lucas3oo opened this issue Aug 9, 2014 · 3 comments

Comments

@Lucas3oo
Copy link

Lucas3oo commented Aug 9, 2014

Hi!

I am also doing JUnit extension/runner and I discovered your nice lib so I wanted to test my stuff with yours.

But filters doesn't seems to work as it should. Also the rule for test name seems to be broken. I file another issue for that. JUnits own parameter support works with filters.

I have some small test that you can run.
Here is the test (sorry I used your package name):

package junitparams;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.manipulation.Filter;

public class FilterTest {
  public static JUnitCore sJUnitCore = new JUnitCore();

  @Test
  public void testFilter() throws Throwable {
    // just so that you can see what the "names" would be
    Description description = createDescription(TestSampleJunit4JUnitParams.class);
    for (Description childDesc : description.getChildren()) {
      System.out.println(childDesc.getDisplayName());
      for (Description childOfChild : childDesc.getChildren()) {
        System.out.println(childOfChild.getMethodName());
      }
    }

    Result result = runTest(TestSampleJunit4JUnitParams.class, "[0] Person of age: 13, false (isAdult)");
    System.out.println("result:" +  result.getFailures());
    assertEquals(0, result.getFailureCount());
    assertEquals(1, result.getRunCount());
  }

  public Result runTest(Class<?> testClass, String methodName) throws Throwable {
    Filter filter = Filter.matchMethodDescription(Description.createTestDescription(testClass, methodName));
    Request request = Request.aClass(testClass);
    request = request.filterWith(filter);
    Result result = sJUnitCore.run(request);
    return result;
  }  

  protected Description createDescription(Class<?> testClass) throws ClassNotFoundException {
    Request request = Request.aClass(testClass);
    Description description = request.getRunner().getDescription();
    return description;
  }
}

And here is the sample test that uses the runner:

package junitparams;

import static junitparams.JUnitParamsRunner.$;
import static org.junit.Assert.assertEquals;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;


@RunWith(JUnitParamsRunner.class)
public class TestSampleJunit4JUnitParams {

  @Rule
  public TestName mName = new TestName();

  public String getName() {
    return mName.getMethodName();
  }

  @Test
  @Parameters(method = "parametersForIsAdult")
  public void isAdult(PersonX person, boolean valid) throws Exception {
    System.out.println(mName);
    System.out.println("in isAdult " + getName() + " using thread "
        + Thread.currentThread().getName() + " person:" + person);
    assertEquals(valid, person.isAdult());
  }

  @SuppressWarnings("unused")
  private Object[] parametersForIsAdult() {
    return $(
                 $(new PersonX(13), false),
                 $(new PersonX(22), true)
            );
  }

}

class PersonX {
  private int age;

  public PersonX(int age) {
    this.age = age;
  }

  public boolean isAdult() {
    return age >= 18;
  }

  @Override
  public String toString() {
    return "Person of age: " + age;
  }

}

The error I got is like this:

result:[initializationError(org.junit.runner.manipulation.Filter): No tests found matching Method [0] Person of age: 13, false (isAdult)(junitparams.TestSampleJunit4JUnitParams) from org.junit.internal.requests.ClassRequest@1a86f2f1]

If you happen to use Eclipse then you can see that filters doesn't work to.
First select any test class that uses the runner and do "Run as JUnit". It will run fine. But when you want to just re-run one of the parameterized tests (one of the "children") then it will not work. (in the JUnit runner view)

I actually tried to see if I could fix it but alas I got a bit confused about the concepts in JUnit4 ;-)
I suspect that the root cause is that the JUnitParamsRunner extends BlockJUnit4ClassRunner which in turn is a runner for FrameworkMethod(s). In JUnitParamsRunner instance for the above test there will only be one (1) FrameworkMethod, isAdult(junitparams.TestSampleJunit4JUnitParams) but the filter is for [0] Person of age: 13, false (isAdult)(junitparams.TestSampleJunit4JUnitParams).
First I thought that you faked one FrameworkMethod for each combination of method and parameters but then I saw that was nto the case. But maybe that could be a solution.

Cheers
Lucas

@apierzch
Copy link
Contributor

From initial research it seems that solution for eclipse would break support in IntelliJ Idea. Currently when describing a test, we return description for whole method. So when Idea is asked to run test which is parametrized (e.g. "myTest"), it asks for description of each test separately. Currently it delegates to BlockJUnit4ClassRunner which describes them without information about parameters. If we override it to return parametrized description, we would break case described, allowing to run single parameterized test case.

@pmelici
Copy link

pmelici commented Aug 27, 2016

Here is another workaround I found to get this working in Eclipse, so that you can run individual parametrized tests: Window | Preferences, Java -> Installed JREs. Pick your default JRE and click Edit. In Default VM arguments, add -DJUnitParams.flat.

This makes the parametrized test appear as one item in the JUnit view. It works, but the downside is you cannot see the parameters.

@jingzhongyue
Copy link

so... Eclipse user cannot use JUnitParams.....

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

No branches or pull requests

4 participants