Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
754b7672
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
754b7672
编写于
7月 29, 2015
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Document Spring's JUnit Rules in the reference manual
Issue: SPR-13037
上级
bfdf6b7b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
89 addition
and
44 deletion
+89
-44
src/asciidoc/testing.adoc
src/asciidoc/testing.adoc
+85
-30
src/asciidoc/whats-new.adoc
src/asciidoc/whats-new.adoc
+4
-14
未找到文件。
src/asciidoc/testing.adoc
浏览文件 @
754b7672
...
...
@@ -993,8 +993,9 @@ instead of `@PostConstruct` and `@PreDestroy`.
[[integration-testing-annotations-junit]]
==== Spring JUnit Testing Annotations
The following annotations are __only__ supported when used in conjunction with the
<<testcontext-junit4-runner,SpringJUnit4ClassRunner>> or the
<<testcontext-support-classes-junit4,JUnit>> support classes.
<<testcontext-junit4-runner,SpringJUnit4ClassRunner>>,
<<testcontext-junit4-rules,Spring'
s
JUnit
rules
>>,
or
<<
testcontext
-
support
-
classes
-
junit4
,
Spring
's JUnit support classes>>.
* `@IfProfileValue`
...
...
@@ -3589,8 +3590,87 @@ be automatically rolled back by the `TransactionalTestExecutionListener` (see
[[testcontext-support-classes]]
==== TestContext Framework support classes
[[testcontext-junit4-runner]]
===== Spring JUnit Runner
The __Spring TestContext Framework__ offers full integration with JUnit 4.9+ through a
custom runner (supported on JUnit 4.9 through 4.12). By annotating test classes with
`@RunWith(SpringJUnit4ClassRunner.class)`, developers can implement standard JUnit-based
unit and integration tests and simultaneously reap the benefits of the TestContext
framework such as support for loading application contexts, dependency injection of test
instances, transactional test method execution, and so on. If you would like to use the
Spring TestContext Framework with an alternative runner such as JUnit'
s
`
Parameterized
`
or
third
-
party
runners
such
as
the
`
MockitoJUnitRunner
`,
you
may
optionally
use
<<
testcontext
-
junit4
-
rules
,
Spring
's support for JUnit rules>> instead.
The following code listing displays the minimal requirements for configuring a test class
to run with the custom Spring `Runner`. `@TestExecutionListeners` is configured with an
empty list in order to disable the default listeners, which otherwise would require an
`ApplicationContext` to be configured through `@ContextConfiguration`.
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({})
public class SimpleTest {
@Test
public void testMethod() {
// execute test logic...
}
}
----
[[testcontext-junit4-rules]]
===== Spring JUnit Rules
The `org.springframework.test.context.junit4.rules` package provides the following JUnit
rules.
* `SpringClassRule`
* `SpringMethodRule`
`SpringClassRule` is a JUnit `TestRule` that supports _class-level_ features of the
_Spring TestContext Framework_; whereas, `SpringMethodRule` is a JUnit `MethodRule` that
supports instance-level and method-level features of the _Spring TestContext Framework_.
In contrast to the `SpringJUnit4ClassRunner`, Spring'
s
rule
-
based
JUnit
support
has
the
advantage
that
it
is
independent
of
any
`
org
.
junit
.
runner
.
Runner
`
implementation
and
can
therefore
be
combined
with
existing
alternative
runners
like
JUnit
's `Parameterized` or
third-party runners such as the `MockitoJUnitRunner`.
In order to support the full functionality of the TestContext framework, a
`SpringClassRule` must be combined with a `SpringMethodRule`. The following example
demonstrates the proper way to declare these rules in an integration test.
[source,java,indent=0]
[subs="verbatim,quotes"]
----
// Optionally specify a non-Spring Runner via @RunWith(...)
@ContextConfiguration
public class IntegrationTest {
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Test
public void testMethod() {
// execute test logic...
}
}
----
[[testcontext-support-classes-junit4]]
===== JUnit support classes
The `org.springframework.test.context.junit4` package provides the following support
classes for JUnit-based test cases.
...
...
@@ -3623,39 +3703,14 @@ provides an `executeSqlScript(..)` method for executing SQL scripts against the
====
These classes are a convenience for extension. If you do not want your test classes to be
tied to a Spring-specific class hierarchy, you can configure your own custom test classes
by using `@RunWith(SpringJUnit4ClassRunner.class)`
, `@ContextConfiguration`,
`@TestExecutionListeners`, and so on
.
by using `@RunWith(SpringJUnit4ClassRunner.class)`
or <<testcontext-junit4-rules,Spring'
s
JUnit
rules
>>
.
====
[[testcontext-junit4-runner]]
===== Spring JUnit Runner
The __Spring TestContext Framework__ offers full integration with JUnit 4.9+ through a
custom runner (tested on JUnit 4.9 -- 4.12). By annotating test classes with
`@RunWith(SpringJUnit4ClassRunner.class)`, developers can implement standard JUnit-based
unit and integration tests and simultaneously reap the benefits of the TestContext
framework such as support for loading application contexts, dependency injection of test
instances, transactional test method execution, and so on. The following code listing
displays the minimal requirements for configuring a test class to run with the custom
Spring Runner. `@TestExecutionListeners` is configured with an empty list in order to
disable the default listeners, which otherwise would require an ApplicationContext to be
configured through `@ContextConfiguration`.
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({})
public class SimpleTest {
@Test
public void testMethod() {
// execute test logic...
}
}
----
[[
testcontext
-
support
-
classes
-
testng
]]
=====
TestNG
support
classes
The
`
org
.
springframework
.
test
.
context
.
testng
`
package
provides
the
following
support
classes
for
TestNG
based
test
cases
.
...
...
src/asciidoc/whats-new.adoc
浏览文件 @
754b7672
...
...
@@ -547,21 +547,11 @@ public @interface MyTestConfig {
=== Testing Improvements
* JUnit-based integration tests can now be executed with JUnit rules
instead of the `SpringJUnit4ClassRunner`. This allows Spring-based
integration tests to be run with alternative runners like JUnit's
`Parameterized` or third-party runners such as the
* JUnit-based integration tests can now be executed with JUnit rules instead of the
`SpringJUnit4ClassRunner`. This allows Spring-based integration tests to be run with
alternative runners like JUnit's `Parameterized` or third-party runners such as the
`MockitoJUnitRunner`.
** Spring JUnit rule configuration requires the following two rules.
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
----
** See <<testcontext-junit4-rules>> for details.
* The Spring MVC Test framework now provides first-class support for HtmlUnit,
including integration with Selenium's WebDriver, allowing for page-based
web application testing without the need to deploy to a Servlet container.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录