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

Add support for parameterized JUnit 4 tests in the Spring TestContext Framework [SPR-5292] #9965

Closed
spring-projects-issues opened this issue Nov 12, 2008 · 23 comments
Assignees
Labels
has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import in: test Issues in the test module status: duplicate A duplicate of another issue type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Nov 12, 2008

Tuomas Kiviaho opened SPR-5292 and commented

SpringJUnit4ClassRunner is unaware of @Parameters annotation that normally is processed with JUnit's own Parameterized runner.


Affects: 2.5.6

Attachments:

Issue Links:

Referenced from: commits spring-attic/spring-framework-issues@b9c8f10, spring-attic/spring-framework-issues@239b3fe

42 votes, 46 watchers

@spring-projects-issues
Copy link
Collaborator Author

Jon Burgin commented

Here is our solution. Enjoy!

@spring-projects-issues
Copy link
Collaborator Author

Jon Burgin commented

That is our solution is attached above. :)

@spring-projects-issues
Copy link
Collaborator Author

Mark van der Voort commented

I think there are a few problems with the solution sketched.

  • It is not compatible with junit4.7
  • Testcases will be treated as testclasses, so any class level DirtiesContext annotations will cause the the context to be refreshed after each case, rather than after the entire set

@spring-projects-issues
Copy link
Collaborator Author

Matt Brown commented

Just curious - should the value of the Fix Version (which has been bumped from 3.0 RCs to 3.1 milestone) be interpreted as meaning that this support will be added in 3.1?

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Indeed, we're usually setting the fix version as a kind of work plan. So in this case, the plan is indeed to provide this in 3.1 M2 towards the end of this year.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Manjuka Soysa commented

As someone pointed out, the previous file does not work with the latest JUnit.
The new SpringParameterizedRunner is hopefully useful for you until @Parameterized is officially supported by Spring.

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

A better approach to this might be to provide the Spring integration test support as a @Rule (as well as or instead of a custom Runner). There might be some limitations of that approach, but it seems worth a try. I know Iwein did this in a blog a while ago, and although he didn't complete the feature set it was pretty easy to get off the ground.

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

We ought to consider parameterization of profile combinations here as well. e.g.: run the same @Test with profiles [p1, p2] active, and run it again with [p2, p3] active, etc.

@spring-projects-issues
Copy link
Collaborator Author

Josef Eisl commented

I've merged Springs JUnit4 Runner (3.0.4) with JUnits Parameterized Runner (r4.8.2). Working just fine for me but not extensively tested.

@spring-projects-issues
Copy link
Collaborator Author

Marek Pietrasz commented

I am afraid current patch doesn't work with TestExecutionListener - I couldn't force it to execute beforeTestClass method.

@spring-projects-issues
Copy link
Collaborator Author

Paris Holley commented

SpringJUnit4ParameterizedClassRunner.java currently has a createTest() implementation that instantiates a new instance of the test class for each method, is this intended behavior? I ended up changing it for our needs to only create it once (so that it behaves like normal non-parameterized tests).

@spring-projects-issues
Copy link
Collaborator Author

Ryan Stewart commented

If I understand you correctly, that's not how JUnit behaves for non-parameterized tests. It always creates a new test instance for each test method. So yes, that's the intended behavior.

@spring-projects-issues
Copy link
Collaborator Author

Paris Holley commented

Ryan you are right, taking another look at javadoc for Parameterized it says its a new instance per test, probably need to take issue up with JUnit. Such behavior makes maintaining test state harder, but can be worked around.

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

Repro project at spring-attic/spring-framework-issues#28. Thanks, Dave.

@spring-projects-issues
Copy link
Collaborator Author

Flemming Jønsson commented

Hi Chris - I noticed your comment from a while back on this JIRA "We ought to consider parameterization of profile combinations here as well. e.g.: run the same @Test with profiles [p1, p2] active, and run it again with [p2, p3] active, etc."

I find that a very useful feature. It just so happens that we have a setup where we need this feature. So I did a small proof of concept using the SpringContextRule from spring-attic/spring-framework-issues#28 and described my approach in the SpringSource fora.

It is very rough and it requires the use of the @Parameterized.Parameters array to supply the list of profiles per test iteration. However ugly an approach it may be it works fine and serves my purpose. So I thought that others might have a similar need and posted my approach here.

Feel free to criticize or suggest improvements. Next time I don't have anything to work on during my commute I will have a look at how to make this possible using the SpringJunit4 runner.
http://forum.springsource.org/showthread.php?129462-RunWith(Parameterized-class)-with-different-set-of-Spring-profiles-per-parameter

@spring-projects-issues
Copy link
Collaborator Author

Ib commented

None of the presently attached junit runners worked for me for one reason or another. As such, I wrote a runner that met my needs. I thought others might benefit from it, hence my sharing it here - by me attaching a file named SpringJUnit4ParameterizedClassRunner.java.v2

@spring-projects-issues
Copy link
Collaborator Author

Gaetan Pitteloud commented

I just submitted the pull request 277 for this issue, in the form of a single runner that is able to run both parameterized and non-parameterized tests in the TestContext framework. I developed it some times ago and have been using it since then without any issue. The runner does not contain the spring-profile feature Chris Beams is talking about, but only "raw" parameterized tests.

@spring-projects-issues
Copy link
Collaborator Author

Ruud Senden commented

It is not exactly a solution for the described problem, but the attached ConstructorArgFromSpringConfigTestRunner.java takes a different approach for parameterizing a unit test. Test classes will need to define a single constructor taking a single parameter. The runner will then run the test class once for every Spring bean that matches the constructor parameter type.

Please note that this class currently only performs dependency injection; for example it doesn't handle Spring Test annotations like ExpectedException, Repeat etc.

@spring-projects-issues
Copy link
Collaborator Author

Ruud Senden commented

Run test once for each Spring bean that matches the single test constructor parameter type.

@spring-projects-issues
Copy link
Collaborator Author

Nikolay Blindov commented

I have attached junit runner (a file namedParameterizedDirtiesContext.java) worked for me. My ruuner is inheritor of junit Parameterized ruuner and uses inheritor of SpringJUnit4ClassRunner for creation a test. So, the runner uses junit approach for parameterizing a unit test and support all SpringJUnit functionalites. The ruuner work with junit 4.8.2 and or higher version.

@spring-projects-issues
Copy link
Collaborator Author

Marko Bjelac commented

I tried several runners attached here, but confirm that only [^SpringJUnit4ParameterizedClassRunner.java.v2] works for me.

@spring-projects-issues
Copy link
Collaborator Author

Koen Serry commented

Since a pull request is created (and is confirmed to work), is there any reason not to merge it?

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jun 24, 2015

Sam Brannen commented

I am resolving this issue as essentially a duplicate of #12387, since #12387 introduced support for integrating the Spring TestContext Framework into JUnit via Rules instead of a Runner.

In other words, thanks to the support for rules introduced in #12387, there is no longer a need for a custom extension of SpringJUnit4ClassRunner that supports parameterized tests... since you can now simply use JUnit's Parameterized runner with Spring's rules.

See ParameterizedSpringRuleTests for a concrete example.

@spring-projects-issues spring-projects-issues added in: test Issues in the test module type: enhancement A general enhancement has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import status: duplicate A duplicate of another issue labels Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import in: test Issues in the test module status: duplicate A duplicate of another issue type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants