@@ -993,8 +993,9 @@ instead of `@PostConstruct` and `@PreDestroy`.
993
993
[[integration-testing-annotations-junit]]
994
994
==== Spring JUnit Testing Annotations
995
995
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>>.
998
999
999
1000
* `@IfProfileValue`
1000
1001
@@ -3589,8 +3590,87 @@ be automatically rolled back by the `TransactionalTestExecutionListener` (see
3589
3590
[[testcontext-support-classes]]
3590
3591
==== TestContext Framework support classes
3591
3592
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
+
3592
3671
[[testcontext-support-classes-junit4]]
3593
3672
===== JUnit support classes
3673
+
3594
3674
The `org.springframework.test.context.junit4` package provides the following support
3595
3675
classes for JUnit-based test cases.
3596
3676
@@ -3623,39 +3703,14 @@ provides an `executeSqlScript(..)` method for executing SQL scripts against the
3623
3703
====
3624
3704
These classes are a convenience for extension. If you do not want your test classes to be
3625
3705
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>> .
3628
3708
====
3629
3709
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
- ----
3656
3710
3657
3711
[[testcontext-support-classes-testng]]
3658
3712
===== TestNG support classes
3713
+
3659
3714
The `org.springframework.test.context.testng` package provides the following support
3660
3715
classes for TestNG based test cases.
3661
3716
0 commit comments