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

Parameterized runner factory #773

Merged
merged 4 commits into from
Apr 18, 2014

Conversation

stefanbirkner
Copy link
Contributor

This is a follow-up to 61badf2 (Pull Request #564), that added a new method protected Runner createRunner(String pattern, int index, Object[] parameters). This pull request provides a different way for building own runners. You have to create a ParametersRunnerFactory that creates the runners and build your own Parameterized runner with this factory.

IMHO this is a better design because the responsibility for creating the runner for a single data set is separated from the Parameterized runner.

It would be nice to have this pull request in JUnit 4.12. Otherwise the createRunner will be part of Parameterized's public API.

@marcphilipp
Copy link
Member

I think that's a better design, too!

Have you considered using an additional annotation on the test class that specifies the factory instead of subclassing Parameterized? Or an annotated field in the test class for that purpose?

@stefanbirkner
Copy link
Contributor Author

I did not consider an annotation, but that's a good idea. I add this feature. It will look like

@RunWith(Parameterized.class)
@UseParametersRunnerFactory(MyFactory.class)
public class MyParameterizedTest {
  ...

Do you think we should still support the constructor protected Parameterized(Class<?> klass, ParametersRunnerFactory runnerFactory)? I would like to remove it until somebody requests it, because currently it is not needed.

@stefanbirkner
Copy link
Contributor Author

I added the annotation.

I still would like to remove the newly added constructor protected Parameterized(Class<?> klass, ParametersRunnerFactory runnerFactory). Any objections?

* runner that it should use your factory.
*
* <pre>
* &#064;RunWith(YourParameterizedRunner.class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should reference Parameterized, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Done.

@marcphilipp
Copy link
Member

I've added a couple of comments inline. Go ahead and remove the extra constructor if you want, your call! :-)


@Override
public boolean equals(Object obj) {
if (this == obj)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JUnit style is to always use braces for "if" "do" and "while" statements.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@stefanbirkner
Copy link
Contributor Author

Applied code review except exception handling for getParametersRunnerFactory (see comment).

@Override
public int hashCode() {
int prime = 14747;
int result = prime + ((fName == null) ? 0 : fName.hashCode());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fName can't be null, so this can be simpler

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@kcooney
Copy link
Member

kcooney commented Feb 25, 2014

LGTM, but I don't use Parameterized so I'm not the best person to review.

Note that github thinks this can't be merged as-is.

@stefanbirkner
Copy link
Contributor Author

I rebase this pull request in the evening.

It is common to add tests to the same package as the class under test.
@stefanbirkner stefanbirkner added this to the 4.12 milestone Feb 26, 2014
This class keeps the data together that are needed for creating a runner for a single data set of a parameterized test. This makes it also clear, that the computation of the name is not the responsibility of the runner but of the Parameterized class.
Separate the class from the Parameterized runner. Makes it clear, that it is designed for reuse.
…meters.

There's now an explicit component for creating the runner of a single set of parameters. IMHO this is a better extension point than overriding a method.

The factory can be specified by the @UseParametersRunnerFactory
annotation or you can subclass the Parameterized class and provide
the factory to its constructor.
@stefanbirkner
Copy link
Contributor Author

Ready to be merged. Thanks everybody for finding the code smells.

@kcooney
Copy link
Member

kcooney commented Apr 17, 2014

@marcphilipp are you okay merging this?

@marcphilipp
Copy link
Member

Yep, go ahead!

kcooney added a commit that referenced this pull request Apr 18, 2014
@kcooney kcooney merged commit 63743d5 into junit-team:master Apr 18, 2014
@kcooney
Copy link
Member

kcooney commented Apr 18, 2014

@stefanbirkner can you update the release notes?

@stefanbirkner stefanbirkner deleted the parameterized-runner-factory branch April 19, 2014 09:03
@EarthCitizen
Copy link
Contributor

Is there an example anywhere of a custom parameters runner using this new factory?

* <h3>Create different runners</h3>
* <p>
* By default the {@code Parameterized} runner creates a slightly modified
* {@link BlockJUnit4ClassRunner} for each set of parameters. You can build an
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the doc should read "You can build your own" instead of "You can build an own". Correct?

@vimil
Copy link
Contributor

vimil commented Jun 15, 2014

Would it be a good idea to have @inherited on the UseParametersRunnerFactory annotation as the @RunWith annotation has @inherited on it.

@kcooney
Copy link
Member

kcooney commented Jun 15, 2014

@vimil good catch! Would you be willing to send us a pull request? If so, please include a test case that fails currently but would pass if UseParametersRunnerFactory was annotated with @Inherited

@vimil
Copy link
Contributor

vimil commented Jun 16, 2014

@kcooney, I've sent a pull request :)

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

Successfully merging this pull request may close these issues.

5 participants