Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
1be29d59
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,发现更多精彩内容 >>
提交
1be29d59
编写于
5月 09, 2019
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Document automatic constructor injection in JUnit Jupiter
Closes gh-22928
上级
c601ecf1
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
67 addition
and
6 deletion
+67
-6
src/docs/asciidoc/testing.adoc
src/docs/asciidoc/testing.adoc
+67
-6
未找到文件。
src/docs/asciidoc/testing.adoc
浏览文件 @
1be29d59
...
...
@@ -1189,6 +1189,7 @@ The following annotations are supported only when used in conjunction with the
* <<integration-testing-annotations-junit-jupiter-springjunitconfig>>
* <<integration-testing-annotations-junit-jupiter-springjunitwebconfig>>
* <<integration-testing-annotations-testconstructor>>
* <<integration-testing-annotations-junit-jupiter-enabledif>>
* <<integration-testing-annotations-junit-jupiter-disabledif>>
...
...
@@ -1281,6 +1282,35 @@ See <<testcontext-ctx-management>> as well as the javadoc for
{api-spring-framework}/test/context/web/WebAppConfiguration.html[`@WebAppConfiguration`]
for further details.
[[integration-testing-annotations-testconstructor]]
===== `@TestConstructor`
`@TestConstructor` is a type-level annotation that is used to configure whether a test
class constructor should be automatically autowired from components in the test'
s
`
ApplicationContext
`.
If
`@
TestConstructor
`
is
not
present
or
meta
-
present
on
a
test
class
,
the
default
_test
constructor
autowire_
mode
will
be
used
.
See
the
tip
below
for
details
on
how
to
change
the
default
mode
.
Note
,
however
,
that
a
local
declaration
of
`@
Autowired
`
on
a
constructor
takes
precedence
over
both
`@
TestConstructor
`
and
the
default
mode
.
.
Configuring
the
default
test
constructor
autowire
mode
[
TIP
]
=====
The
default
_test
constructor
autowire_
mode
can
be
configured
by
setting
the
`
spring
.
test
.
constructor
.
autowire
`
JVM
system
property
to
`
true
`.
Alternatively
,
the
default
mode
may
be
configured
via
the
`
SpringProperties
`
mechanism
.
If
the
`
spring
.
test
.
constructor
.
autowire
`
property
is
not
set
,
test
class
constructors
will
not
be
automatically
autowired
.
=====
NOTE
:
As
of
Spring
Framework
5.2
,
`@
TestConstructor
`
is
only
supported
in
conjunction
with
the
`
SpringExtension
`
for
use
with
JUnit
Jupiter
.
Note
that
the
`
SpringExtension
`
is
often
automatically
registered
for
you
–
for
example
,
when
using
annotations
such
as
`@
SpringJUnitConfig
`
and
`@
SpringJUnitWebConfig
`
or
various
test
-
related
annotations
from
Spring
Boot
Test
.
[[
integration
-
testing
-
annotations
-
junit
-
jupiter
-
enabledif
]]
=====
`@
EnabledIf
`
...
...
@@ -1386,6 +1416,7 @@ You can use each of the following as a meta-annotation in conjunction with the
*
`@
ProfileValueSourceConfiguration
`
_
(
only
supported
on
JUnit
4
)
_
*
`@
SpringJUnitConfig
`
_
(
only
supported
on
JUnit
Jupiter
)
_
*
`@
SpringJUnitWebConfig
`
_
(
only
supported
on
JUnit
Jupiter
)
_
*
`@
TestConstructor
`
_
(
only
supported
on
JUnit
Jupiter
)
_
*
`@
EnabledIf
`
_
(
only
supported
on
JUnit
Jupiter
)
_
*
`@
DisabledIf
`
_
(
only
supported
on
JUnit
Jupiter
)
_
...
...
@@ -4423,15 +4454,25 @@ Specifically, `SpringExtension` can inject dependencies from the test's
[[
testcontext
-
junit
-
jupiter
-
di
-
constructor
]]
======
Constructor
Injection
If a parameter in a constructor for a JUnit Jupiter test class is of type
If
a
specific
parameter
in
a
constructor
for
a
JUnit
Jupiter
test
class
is
of
type
`
ApplicationContext
`
(
or
a
sub
-
type
thereof
)
or
is
annotated
or
meta
-
annotated
with
`@
Autowired
`,
`@
Qualifier
`,
or
`@
Value
`,
Spring
injects
the
value
for
that
specific
parameter with the corresponding bean from the test'
s
`
ApplicationContext
`.
You
can
also
directly
annotate
a
test
constructor
with
`@
Autowired
`
if
all
of
the
parameters
should
be
supplied
by
Spring
.
parameter
with
the
corresponding
bean
or
value
from
the
test
's `ApplicationContext`.
Spring can also be configured to autowire all arguments for a test class constructor if
the constructor is considered to be _autowirable_. A constructor is considered to be
autowirable if one of the following conditions is met (in order of precedence).
* The constructor is annotated with `@Autowired`.
* `@TestConstructor` is present or meta-present on the test class with the `autowire`
attribute set to `true`.
* The default _test constructor autowire_ mode is set to `true`.
See <<integration-testing-annotations-testconstructor>> for details on the use of
`@TestConstructor` and how to set the global _test constructor autowire_ mode.
WARNING
:
If
the
constructor
for
a
test
class
is
itself
annotated
with
`@
Autowired
`,
Spring
assumes
the
responsibility
for
resolving
_all_
parameters
in
the
constructor
.
WARNING: If the constructor for a test class is
considered to be _autowirable_, Spring
assumes the responsibility for resolving arguments for all
parameters in the constructor.
Consequently, no other `ParameterResolver` registered with JUnit Jupiter can resolve
parameters for such a constructor.
...
...
@@ -4477,6 +4518,26 @@ In the following example, Spring injects the `OrderService` bean from the
Note that this feature lets test dependencies be `final` and therefore immutable.
If the `spring.test.constructor.autowire` property is to `true` (see
<<integration-testing-annotations-testconstructor>>), we can omit the declaration of
`@Autowired` on the constructor in the previous example resulting in the following.
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@SpringJUnitConfig(TestConfig.class)
class OrderServiceIntegrationTests {
private final OrderService orderService;
OrderServiceIntegrationTests(OrderService orderService) {
this.orderService = orderService.
}
// tests that use the injected OrderService
}
----
[[testcontext-junit-jupiter-di-method]]
====== Method Injection
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录