Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
d3129a8b
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,发现更多精彩内容 >>
提交
d3129a8b
编写于
9月 27, 2017
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Convert selected examples to JUnit Jupiter in reference manual
Issue: SPR-14524
上级
8c9d42f7
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
39 addition
and
45 deletion
+39
-45
src/docs/asciidoc/testing.adoc
src/docs/asciidoc/testing.adoc
+39
-45
未找到文件。
src/docs/asciidoc/testing.adoc
浏览文件 @
d3129a8b
...
@@ -3552,24 +3552,23 @@ which references a URL (e.g., a path prefixed with `classpath:`, `file:`, `http:
...
@@ -3552,24 +3552,23 @@ which references a URL (e.g., a path prefixed with `classpath:`, `file:`, `http:
will
be
loaded
using
the
specified
resource
protocol
.
will
be
loaded
using
the
specified
resource
protocol
.
The
following
example
demonstrates
how
to
use
`@
Sql
`
at
the
class
level
and
at
the
method
The
following
example
demonstrates
how
to
use
`@
Sql
`
at
the
class
level
and
at
the
method
level
within
a
JUnit
4
based
integration
test
class
.
level
within
a
JUnit
Jupiter
based
integration
test
class
.
[
source
,
java
,
indent
=
0
]
[
source
,
java
,
indent
=
0
]
[
subs
=
"verbatim,quotes"
]
[
subs
=
"verbatim,quotes"
]
----
----
@
RunWith
(
SpringRunner
.
class
)
@
SpringJUnitConfig
@
ContextConfiguration
@
Sql
(
"/test-schema.sql"
)
@
Sql
(
"/test-schema.sql"
)
public
class
DatabaseTests
{
class
DatabaseTests
{
@
Test
@
Test
public
void
emptySchemaTest
{
void
emptySchemaTest
{
//
execute
code
that
uses
the
test
schema
without
any
test
data
//
execute
code
that
uses
the
test
schema
without
any
test
data
}
}
@
Test
@
Test
@
Sql
({
"/test-schema.sql"
,
"/test-user-data.sql"
})
@
Sql
({
"/test-schema.sql"
,
"/test-user-data.sql"
})
public
void
userTest
{
void
userTest
{
//
execute
code
that
uses
the
test
schema
and
test
data
//
execute
code
that
uses
the
test
schema
and
test
data
}
}
}
}
...
@@ -3699,41 +3698,41 @@ executed in an isolated transaction. Although a thorough discussion of all suppo
...
@@ -3699,41 +3698,41 @@ executed in an isolated transaction. Although a thorough discussion of all suppo
options for transaction management with `@Sql` is beyond the scope of this reference
options for transaction management with `@Sql` is beyond the scope of this reference
manual, the javadocs for `@SqlConfig` and `SqlScriptsTestExecutionListener` provide
manual, the javadocs for `@SqlConfig` and `SqlScriptsTestExecutionListener` provide
detailed information, and the following example demonstrates a typical testing scenario
detailed information, and the following example demonstrates a typical testing scenario
using JUnit
4 and transactional tests with `@Sql`. Note that there is no need to clean up
using JUnit
Jupiter and transactional tests with `@Sql`. Note that there is no need to
the database after the `usersTest()` method is executed since any changes made to th
e
clean up the database after the `usersTest()` method is executed since any changes mad
e
database (either within the test method or within the `/test-data.sql` script) will
to the database (either within the test method or within the `/test-data.sql` script)
be automatically rolled back by the `TransactionalTestExecutionListener` (see
will
be automatically rolled back by the `TransactionalTestExecutionListener` (see
<<testcontext-tx,transaction management>> for details).
<<testcontext-tx,transaction management>> for details).
[source,java,indent=0]
[source,java,indent=0]
[subs="verbatim,quotes"]
[subs="verbatim,quotes"]
----
----
@RunWith(SpringRunner.class)
@SpringJUnitConfig(TestDatabaseConfig.class)
@ContextConfiguration(classes = TestDatabaseConfig.class)
@Transactional
@Transactional
public
class TransactionalSqlScriptsTests {
class TransactionalSqlScriptsTests {
protected
JdbcTemplate jdbcTemplate;
final
JdbcTemplate jdbcTemplate;
@Autowired
@Autowired
public void setDataSource
(DataSource dataSource) {
TransactionalSqlScriptsTests
(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
}
@Test
@Test
@Sql("/test-data.sql")
@Sql("/test-data.sql")
public
void usersTest() {
void usersTest() {
// verify state in test database:
// verify state in test database:
assertNumUsers(2);
assertNumUsers(2);
// execute code that uses the test data...
// execute code that uses the test data...
}
}
protected
int countRowsInTable(String tableName) {
int countRowsInTable(String tableName) {
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
}
}
protected void assertNumUsers(int expected) {
void assertNumUsers(int expected) {
assertEquals("Number of rows in the [user] table.", expected, countRowsInTable("user"));
assertEquals(expected, countRowsInTable("user"),
"Number of rows in the [user] table.");
}
}
}
}
----
----
...
@@ -4095,9 +4094,8 @@ use of `@RepeatedTest` from JUnit Jupiter allows the test method to gain access
...
@@ -4095,9 +4094,8 @@ use of `@RepeatedTest` from JUnit Jupiter allows the test method to gain access
class
OrderServiceIntegrationTests
{
class
OrderServiceIntegrationTests
{
@
RepeatedTest
(
10
)
@
RepeatedTest
(
10
)
void
placeOrderRepeatedly
(
void
placeOrderRepeatedly
(
RepetitionInfo
repetitionInfo
,
@
Autowired
OrderService
orderService
,
@
Autowired
OrderService
orderService
)
{
RepetitionInfo
repetitionInfo
)
{
//
use
orderService
from
the
test
's ApplicationContext
//
use
orderService
from
the
test
's ApplicationContext
// and repetitionInfo from JUnit Jupiter
// and repetitionInfo from JUnit Jupiter
...
@@ -4194,37 +4192,33 @@ of the Servlet API>> available in the `spring-test` module. This allows performi
...
@@ -4194,37 +4192,33 @@ of the Servlet API>> available in the `spring-test` module. This allows performi
requests and generating responses without the need for running in a Servlet container.
requests and generating responses without the need for running in a Servlet container.
For the most part everything should work as it does at runtime with a few notable
For the most part everything should work as it does at runtime with a few notable
exceptions as explained in <<spring-mvc-test-vs-end-to-end-integration-tests>>. Here is a
exceptions as explained in <<spring-mvc-test-vs-end-to-end-integration-tests>>. Here is a
JUnit
4
based example of using Spring MVC Test:
JUnit
Jupiter
based example of using Spring MVC Test:
[source,java,indent=0]
[source,java,indent=0]
----
----
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringRunner.class)
@SpringJUnitWebConfig(locations = "test-servlet-context.xml")
@WebAppConfiguration
class ExampleTests {
@ContextConfiguration("test-servlet-context.xml")
public class ExampleTests {
@Autowired
private MockMvc mockMvc;
private WebApplicationContext wac;
private MockMvc mockMvc;
@BeforeEach
void setup(WebApplicationContext wac) {
@Before
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
public void setup() {
}
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
@Test
public void getAccount() throws Exception {
void getAccount() throws Exception {
this.mockMvc.perform(get("/accounts/1").accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
this.mockMvc.perform(get("/accounts/1")
.andExpect(status().isOk())
.accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
.andExpect(content().contentType("application/json"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("Lee"));
.andExpect(content().contentType("application/json"))
}
.andExpect(jsonPath("$.name").value("Lee"));
}
}
}
----
----
The above test relies on the `WebApplicationContext` support of the __TestContext framework__
The above test relies on the `WebApplicationContext` support of the __TestContext framework__
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录