Skip to content

Commit 754b767

Browse files
committed
Document Spring's JUnit Rules in the reference manual
Issue: SPR-13037
1 parent bfdf6b7 commit 754b767

File tree

2 files changed

+89
-44
lines changed

2 files changed

+89
-44
lines changed

src/asciidoc/testing.adoc

Lines changed: 85 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -993,8 +993,9 @@ instead of `@PostConstruct` and `@PreDestroy`.
993993
[[integration-testing-annotations-junit]]
994994
==== Spring JUnit Testing Annotations
995995
The following annotations are __only__ supported when used in conjunction with the
996-
<<testcontext-junit4-runner,SpringJUnit4ClassRunner>> or the
997-
<<testcontext-support-classes-junit4,JUnit>> support classes.
996+
<<testcontext-junit4-runner,SpringJUnit4ClassRunner>>,
997+
<<testcontext-junit4-rules,Spring's JUnit rules>>, or
998+
<<testcontext-support-classes-junit4,Spring's JUnit support classes>>.
998999

9991000
* `@IfProfileValue`
10001001

@@ -3589,8 +3590,87 @@ be automatically rolled back by the `TransactionalTestExecutionListener` (see
35893590
[[testcontext-support-classes]]
35903591
==== TestContext Framework support classes
35913592

3593+
3594+
[[testcontext-junit4-runner]]
3595+
===== Spring JUnit Runner
3596+
3597+
The __Spring TestContext Framework__ offers full integration with JUnit 4.9+ through a
3598+
custom runner (supported on JUnit 4.9 through 4.12). By annotating test classes with
3599+
`@RunWith(SpringJUnit4ClassRunner.class)`, developers can implement standard JUnit-based
3600+
unit and integration tests and simultaneously reap the benefits of the TestContext
3601+
framework such as support for loading application contexts, dependency injection of test
3602+
instances, transactional test method execution, and so on. If you would like to use the
3603+
Spring TestContext Framework with an alternative runner such as JUnit's `Parameterized`
3604+
or third-party runners such as the `MockitoJUnitRunner`, you may optionally use
3605+
<<testcontext-junit4-rules,Spring's support for JUnit rules>> instead.
3606+
3607+
The following code listing displays the minimal requirements for configuring a test class
3608+
to run with the custom Spring `Runner`. `@TestExecutionListeners` is configured with an
3609+
empty list in order to disable the default listeners, which otherwise would require an
3610+
`ApplicationContext` to be configured through `@ContextConfiguration`.
3611+
3612+
[source,java,indent=0]
3613+
[subs="verbatim,quotes"]
3614+
----
3615+
@RunWith(SpringJUnit4ClassRunner.class)
3616+
@TestExecutionListeners({})
3617+
public class SimpleTest {
3618+
3619+
@Test
3620+
public void testMethod() {
3621+
// execute test logic...
3622+
}
3623+
}
3624+
----
3625+
3626+
3627+
[[testcontext-junit4-rules]]
3628+
===== Spring JUnit Rules
3629+
3630+
The `org.springframework.test.context.junit4.rules` package provides the following JUnit
3631+
rules.
3632+
3633+
* `SpringClassRule`
3634+
* `SpringMethodRule`
3635+
3636+
`SpringClassRule` is a JUnit `TestRule` that supports _class-level_ features of the
3637+
_Spring TestContext Framework_; whereas, `SpringMethodRule` is a JUnit `MethodRule` that
3638+
supports instance-level and method-level features of the _Spring TestContext Framework_.
3639+
3640+
In contrast to the `SpringJUnit4ClassRunner`, Spring's rule-based JUnit support has the
3641+
advantage that it is independent of any `org.junit.runner.Runner` implementation and can
3642+
therefore be combined with existing alternative runners like JUnit's `Parameterized` or
3643+
third-party runners such as the `MockitoJUnitRunner`.
3644+
3645+
In order to support the full functionality of the TestContext framework, a
3646+
`SpringClassRule` must be combined with a `SpringMethodRule`. The following example
3647+
demonstrates the proper way to declare these rules in an integration test.
3648+
3649+
3650+
[source,java,indent=0]
3651+
[subs="verbatim,quotes"]
3652+
----
3653+
// Optionally specify a non-Spring Runner via @RunWith(...)
3654+
@ContextConfiguration
3655+
public class IntegrationTest {
3656+
3657+
@ClassRule
3658+
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
3659+
3660+
@Rule
3661+
public final SpringMethodRule springMethodRule = new SpringMethodRule();
3662+
3663+
@Test
3664+
public void testMethod() {
3665+
// execute test logic...
3666+
}
3667+
}
3668+
----
3669+
3670+
35923671
[[testcontext-support-classes-junit4]]
35933672
===== JUnit support classes
3673+
35943674
The `org.springframework.test.context.junit4` package provides the following support
35953675
classes for JUnit-based test cases.
35963676

@@ -3623,39 +3703,14 @@ provides an `executeSqlScript(..)` method for executing SQL scripts against the
36233703
====
36243704
These classes are a convenience for extension. If you do not want your test classes to be
36253705
tied to a Spring-specific class hierarchy, you can configure your own custom test classes
3626-
by using `@RunWith(SpringJUnit4ClassRunner.class)`, `@ContextConfiguration`,
3627-
`@TestExecutionListeners`, and so on.
3706+
by using `@RunWith(SpringJUnit4ClassRunner.class)` or <<testcontext-junit4-rules,Spring's
3707+
JUnit rules>>.
36283708
====
36293709

3630-
[[testcontext-junit4-runner]]
3631-
===== Spring JUnit Runner
3632-
The __Spring TestContext Framework__ offers full integration with JUnit 4.9+ through a
3633-
custom runner (tested on JUnit 4.9 -- 4.12). By annotating test classes with
3634-
`@RunWith(SpringJUnit4ClassRunner.class)`, developers can implement standard JUnit-based
3635-
unit and integration tests and simultaneously reap the benefits of the TestContext
3636-
framework such as support for loading application contexts, dependency injection of test
3637-
instances, transactional test method execution, and so on. The following code listing
3638-
displays the minimal requirements for configuring a test class to run with the custom
3639-
Spring Runner. `@TestExecutionListeners` is configured with an empty list in order to
3640-
disable the default listeners, which otherwise would require an ApplicationContext to be
3641-
configured through `@ContextConfiguration`.
3642-
3643-
[source,java,indent=0]
3644-
[subs="verbatim,quotes"]
3645-
----
3646-
@RunWith(SpringJUnit4ClassRunner.class)
3647-
@TestExecutionListeners({})
3648-
public class SimpleTest {
3649-
3650-
@Test
3651-
public void testMethod() {
3652-
// execute test logic...
3653-
}
3654-
}
3655-
----
36563710

36573711
[[testcontext-support-classes-testng]]
36583712
===== TestNG support classes
3713+
36593714
The `org.springframework.test.context.testng` package provides the following support
36603715
classes for TestNG based test cases.
36613716

src/asciidoc/whats-new.adoc

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -547,21 +547,11 @@ public @interface MyTestConfig {
547547

548548
=== Testing Improvements
549549

550-
* JUnit-based integration tests can now be executed with JUnit rules
551-
instead of the `SpringJUnit4ClassRunner`. This allows Spring-based
552-
integration tests to be run with alternative runners like JUnit's
553-
`Parameterized` or third-party runners such as the
550+
* JUnit-based integration tests can now be executed with JUnit rules instead of the
551+
`SpringJUnit4ClassRunner`. This allows Spring-based integration tests to be run with
552+
alternative runners like JUnit's `Parameterized` or third-party runners such as the
554553
`MockitoJUnitRunner`.
555-
** Spring JUnit rule configuration requires the following two rules.
556-
[source,java,indent=0]
557-
[subs="verbatim,quotes"]
558-
----
559-
@ClassRule
560-
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
561-
562-
@Rule
563-
public final SpringMethodRule springMethodRule = new SpringMethodRule();
564-
----
554+
** See <<testcontext-junit4-rules>> for details.
565555
* The Spring MVC Test framework now provides first-class support for HtmlUnit,
566556
including integration with Selenium's WebDriver, allowing for page-based
567557
web application testing without the need to deploy to a Servlet container.

0 commit comments

Comments
 (0)