Skip to content

Junit4 support [SPR-3104] #7791

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

Closed
spring-projects-issues opened this issue Feb 2, 2007 · 8 comments
Closed

Junit4 support [SPR-3104] #7791

spring-projects-issues opened this issue Feb 2, 2007 · 8 comments
Assignees
Labels
has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Feb 2, 2007

Mujahid Ali opened SPR-3104 and commented

Junit4 doesn't require a class to extend TestCase to write unit tests; there needs to be a similar class to AbstractDependencyInjectionSpringContextTests which provides support for tests that depend on a Spring context but doesn't extends from Junit's TestCase.

If there is a workaround for this, please either reply here or in the forum under the thread http://forum.springframework.org/showthread.php?t=34378

Thanks,

-Mujahid


Issue Links:

18 votes, 18 watchers

@spring-projects-issues
Copy link
Collaborator Author

Santiago Valdarrama commented

I create a project to maintain a set of JUnit 4 extensions to test spring code. You can see some information or download binaries or the complete source code from http://code.google.com/p/gienah-testing/. Please, feel free to send your feedback in order to make the project grow.

Maybe Spring developers could take a look to the project to include a similar behaviour in future spring releases.

@spring-projects-issues
Copy link
Collaborator Author

Santiago Valdarrama commented

To see a little tutorial of gienah-testing project, go to http://groups.google.com/group/gienah-testing/web/project-5-minutes-tutorial

@spring-projects-issues
Copy link
Collaborator Author

Rich Eggert commented

I've also been interested in this. I've been holding off on implementing integration tests for my projects, since I don't want to start off using an obsolete API.

I haven't tried it out yet, but the Gienah API looks to be clean and consistent with how JUnit4 and Spring are meant to work. Given that the necessary code is already written as uses the same license as Spring, would it be possible to get it merged into the main Spring codebase in the near future?

@spring-projects-issues
Copy link
Collaborator Author

Santiago Valdarrama commented

I'll be very glad to help with this issue, so, please, if you guys want to get access to the gienah-testing source code, let me know.

@spring-projects-issues
Copy link
Collaborator Author

Ben Turner commented

This is a feature I'd really like to see - currently I have to decide between Spring injection or JUnit4 Parametrized tests.

However, I note the gienah-testing project (above) advocates the use of a SpringRunner to do the Spring bean injection - so I assume this approach would leave me with the same dilemma - SpringRunner OR Parameterized runner ? In my case, I am hoping for an update to the existing Spring Classes to allow simple test injection without limiting my choice of Test Runner.

Can see arguments for both approaches I guess, but wanted to add my 5 cents here - have waited a while for this integration and would be disappointed if it was of little or no use to me.

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

In Spring 2.1 M4 we are introducing the new "Spring TestContext Framework"
which provides annotation-driven unit and integration testing support that
is agnostic of the actual testing framework in use. The same techniques and
annotation-based configuration used in, for example, a JUnit 3.8 environment
can also be applied to tests written with JUnit 4.4, TestNG, etc.

In addition to providing generic and extensible testing infrastructure, the
Spring TestContext Framework provides out-of-the-box support for Spring-specific
integration testing functionality such as context management and caching,
dependency injection of test fixtures, and transactional test management with
default rollback semantics.

To discover how this new testing support can assist you with writing unit and
integration tests, consult the "Spring TestContext Framework" section of the
revised testing chapter in the Spring reference manual.

I encourage everyone to take a look at the examples and give the new testing
support a try with either JUnit 3.8 or JUnit 4.4. Let me know if you run
into any issues or have any ideas for improvement.

Thanks,

Sam

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Hi Mujahid,

Junit4 doesn't require a class to extend TestCase to write unit tests; there needs
to be a similar class to AbstractDependencyInjectionSpringContextTests which
provides support for tests that depend on a Spring context but doesn't extends
from Junit's TestCase.

The Spring TestContext Framework provides a custom runner for JUnit 4.4 called
SpringJUnit4ClassRunner. If you use this Runner, you are free to extend any class
you like or even not to extend any class at all. The choice is entirely yours!

regards,

Sam

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Hi Ben,

This is a feature I'd really like to see - currently I have to decide
between Spring injection or JUnit4 Parametrized tests.

However, I note the gienah-testing project (above) advocates the use
of a SpringRunner to do the Spring bean injection - so I assume this
approach would leave me with the same dilemma - SpringRunner OR
Parameterized runner ? In my case, I am hoping for an update to the
existing Spring Classes to allow simple test injection without
limiting my choice of Test Runner.

That is a very valid point you make. Due to the design of JUnit 4's
Runner and @RunWith architecture, many third-party developers are
essentially forced to provide a custom Runner in order to offer
substantial value-add over and above the standard JUnit feature set.

For the Spring TestContext Framework, we have also chosen to provide
such a custom Runner: SpringJUnit4ClassRunner.

To benefit from the full scope of the new TestContext framework, you
would in fact need to annotate your classes with
@RunWith(SpringJUnit4ClassRunner.class), which would prohibit you from
being able to use the Parameterized Runner simultaneously.

However, you mentioned that you would only like to have "simple test
injection without limiting [your] choice of Test Runner." If that's all
that you require, you can instrument your test class with the
TestContextManager and use the Parameterized Runner at the same time.

I created a proof-of-concept class and checked it into CVS as an internal
test for the Spring test suite. Feel free to check it out to see if
it's what you were looking for. Here's an excerpt from the JavaDoc:

ParameterizedDependencyInjectionTests is a simple JUnit 4 based unit test
which demonstrates how to use JUnit's Parameterized Runner in conjunction
with @ContextConfiguration, the DependencyInjectionTestExecutionListener,
and a TestContextManager to provide dependency injection to a
"parameterized test instance".

In summary, if you choose the manual route using TestContextManager
directly you will not be able to benefit from the full support provided by
the SpringJUnit4ClassRunner, but you will have the flexibility to use a
Runner (e.g., Parameterized) of your choice.

NOTE: I do not want to mislead anyone. The solution mentioned above
does not apply to all TestExecutionListeners. In fact, this solution
is only viable when using the DependencyInjectionTestExecutionListener
alone. If you are testing against JUnit 4 and use
DirtiesContextTestExecutionListener, TransactionalTestExecutionListener,
or any other listener which requires a reference to the actual test method,
you must run your test class with the SpringJUnit4ClassRunner (or find
an alternative means of providing a reference to the executing test method
to the TestContextManager).

Regards,

Sam

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 type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants