- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章SpringBoot使用@SpringBootTest注解开发单元测试教程由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
@SpringBootTest注解是SpringBoot自1.4.0版本开始引入的一个用于测试的注解。基本用法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
<
project
xmlns
=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<
modelVersion
>4.0.0</
modelVersion
>
<
groupId
>com.cxh.test</
groupId
>
<
artifactId
>generator</
artifactId
>
<
version
>0.0.1-SNAPSHOT</
version
>
<
parent
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-parent</
artifactId
>
<
version
>1.5.1.RELEASE</
version
>
<
relativePath
/>
<!-- lookup parent from repository -->
</
parent
>
<
properties
>
<
project.build.sourceEncoding
>UTF-8</
project.build.sourceEncoding
>
<
project.reporting.outputEncoding
>UTF-8</
project.reporting.outputEncoding
>
<
java.version
>1.8</
java.version
>
</
properties
>
<
dependencies
>
<!-- spring boot web 依赖 -->
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-web</
artifactId
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-test</
artifactId
>
</
dependency
>
<!-- 修改后立即生效,热部署 -->
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>springloaded</
artifactId
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-devtools</
artifactId
>
</
dependency
>
<!-- 视图 -->
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-thymeleaf</
artifactId
>
</
dependency
>
<!-- mybatis 依赖 -->
<
dependency
>
<
groupId
>org.mybatis.spring.boot</
groupId
>
<
artifactId
>mybatis-spring-boot-starter</
artifactId
>
<
version
>1.3.0</
version
>
</
dependency
>
<!-- mysql 依赖 -->
<
dependency
>
<
groupId
>mysql</
groupId
>
<
artifactId
>mysql-connector-java</
artifactId
>
<
version
>5.0.4</
version
>
</
dependency
>
<!-- alibaba的druid数据库连接池 -->
<
dependency
>
<
groupId
>com.alibaba</
groupId
>
<
artifactId
>druid-spring-boot-starter</
artifactId
>
<
version
>1.1.9</
version
>
</
dependency
>
</
dependencies
>
<
dependencyManagement
>
<
dependencies
>
<
dependency
>
<
groupId
>org.springframework.cloud</
groupId
>
<
artifactId
>spring-cloud-dependencies</
artifactId
>
<
version
>Camden.SR6</
version
>
<
type
>pom</
type
>
<
scope
>import</
scope
>
</
dependency
>
</
dependencies
>
</
dependencyManagement
>
<
build
/>
</
project
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package
com.cxh.study.platform;
import
org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
* @desciption
* @author ChenXiHua
*
*/
@SpringBootApplication
public
class
GeneratorApp {
/**
* @param args
*/
public
static
void
main(String[] args) {
SpringApplication.run(GeneratorApp.
class
, args);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package
com.cxh.study.platform;
import
org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
* @desciption
* @author ChenXiHua
*
*/
@SpringBootApplication
public
class
GeneratorApp {
/**
* @param args
*/
public
static
void
main(String[] args) {
SpringApplication.run(GeneratorApp.
class
, args);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package
com.cxh.study.platform.service;
import
java.util.List;
import
com.cxh.study.platform.entity.User;
public
interface
IUserService {
// 获取所有用户
List<User> getAll();
// 根据id获取用户
User getUserById(Integer id);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package
com.cxh.study.platform.service.impl;
import
java.util.List;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Service;
import
com.cxh.study.platform.entity.User;
import
com.cxh.study.platform.mapper.IUserMapper;
import
com.cxh.study.platform.service.IUserService;
@Service
(
"userServiceImpl"
)
public
class
UserServiceImpl
implements
IUserService {
@Autowired
private
IUserMapper userMapper;
@Override
public
List<User> getAll() {
return
userMapper.getAll();
}
@Override
public
User getUserById(Integer id) {
return
userMapper.getUserById(id);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package
com.cxh.study.platform.mapper;
import
java.util.List;
import
org.apache.ibatis.annotations.Mapper;
import
com.cxh.study.platform.entity.User;
@Mapper
public
interface
IUserMapper {
//获取所有用户
List<User>getAll();
//根据id获取用户
User getUserById(Integer id);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<
mapper
namespace
=
"com.cxh.study.platform.mapper.IUserMapper"
>
<!--获取所有用户 -->
<
select
id
=
"getAll"
resultType
=
"com.cxh.study.platform.entity.User"
>
SELECT * FROM s_user
</
select
>
<!-- 根据id获取单个用户 -->
<
select
id
=
"getUserById"
parameterType
=
"java.lang.Integer"
resultType
=
"com.cxh.study.platform.entity.User"
>
SELECT * FROM s_user
where id=#{id}
</
select
>
</
mapper
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
package
com.cxh.test;
import
java.net.URL;
import
java.util.List;
import
org.junit.Before;
import
org.junit.Test;
import
org.junit.runner.RunWith;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.boot.context.embedded.LocalServerPort;
import
org.springframework.boot.test.context.SpringBootTest;
import
org.springframework.boot.test.web.client.TestRestTemplate;
import
org.springframework.http.ResponseEntity;
import
org.springframework.test.context.junit4.SpringRunner;
import
com.cxh.study.platform.GeneratorApp;
import
com.cxh.study.platform.entity.User;
/**
*
* @desciption 用户管理测试类
* @author ChenXiHua
* @date 2019年2月19日
*
*/
@RunWith
(SpringRunner.
class
)
@SpringBootTest
(classes = GeneratorApp.
class
, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public
class
UserTest {
/**
* @LocalServerPort 提供了 @Value("${local.server.port}") 的代替
*/
@LocalServerPort
private
int
port;
private
URL base;
@Autowired
private
TestRestTemplate restTemplate;
@Before
public
void
setUp()
throws
Exception {
String url = String.format(
"http://localhost:%d/"
, port);
System.out.println(String.format(
"port is : [%d]"
, port));
this
.base =
new
URL(url);
}
/**
* 向"/test"地址发送请求,并打印返回结果
* @throws Exception
*/
//@Test
public
void
test1()
throws
Exception {
ResponseEntity<String> response =
this
.restTemplate.getForEntity(
this
.base.toString() +
"/test"
, String.
class
,
""
);
System.out.println(String.format(
"测试结果为:%s"
, response.getBody()));
}
//@Test
public
void
getAllTest()
throws
Exception {
ResponseEntity<List> response =
this
.restTemplate.getForEntity(
this
.base.toString() +
"/getAll"
, List.
class
,
""
);
System.out.println(String.format(
"测试结果为:%s"
, response.getBody()));
}
@Test
public
void
getUserByIdTest()
throws
Exception {
ResponseEntity<User> response =
this
.restTemplate.getForEntity(
this
.base.toString() +
"/getUserById?id=1"
, User.
class
,
""
);
System.out.println(String.format(
"测试结果为:%s"
, response.getBody().toString()));
}
}
|
其中,classes属性指定启动类,SpringBootTest.WebEnvironment.RANDOM_PORT经常和测试类中@LocalServerPort一起在注入属性时使用。会随机生成一个端口号.
2019-02-19 16:18:27.933 INFO 6676 --- [ main] com.cxh.test.UserTest : Started UserTest in 25.637 seconds (JVM running for 27.647) 。
port is : [14067] 。
2019-02-19 16:18:31.366 INFO 6676 --- [o-auto-1-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 。
2019-02-19 16:18:31.367 INFO 6676 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 。
2019-02-19 16:18:31.462 INFO 6676 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 95 ms 。
测试结果为:User [id=1, account=cxh, password=123, type=1, status=1] 。
2019-02-19 16:18:35.326 INFO 6676 --- [ Thread-5] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@191004a: startup date [Tue Feb 19 16:18:05 CST 2019]; root of context hierarchy 。
到此这篇关于SpringBoot使用@SpringBootTest注解开发单元测试教程的文章就介绍到这了,更多相关SpringBoot使用@SpringBootTest开发单元测试内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/weixin_39220472/article/details/87714756 。
最后此篇关于SpringBoot使用@SpringBootTest注解开发单元测试教程的文章就讲到这里了,如果你想了解更多关于SpringBoot使用@SpringBootTest注解开发单元测试教程的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在做一个关于代码学院的教程,我在这里收到一个错误,说“看起来你的函数没有返回‘唉,你没有资格获得信用卡。资本主义就是这样残酷。’”当收入参数为 75 时。”但是该字符串在控制台中返回(由于某种原因
我正在阅读 Go 的官方教程,但很难理解 Channel 和 Buffered Channels 之间的区别。教程的链接是 https://tour.golang.org/concurrency/2和
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
作为 iOS 新手,有大量书籍可以满足学习基础知识的需求。现在,我想转向一些高级阅读,例如 OAuth 和 SQLite 以及动态 API 派生的 TableView 等。您可以推荐任何资源吗? 最佳
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 8 年前。
前言 很多同学都知道,我们常见的CTF赛事除了解题赛之外,还有一种赛制叫AWD赛制。在这种赛制下,我们战队会拿到一个或多个服务器。服务器的连接方式通常是SSH链接,并且可能一个战队可能会同时有
Memcached是一个自由开源的,高性能,分布式内存键值对缓存系统 Memcached 是一种基于内存的key-value存储,用来存储小块的任意数据(字符串、对象),这些数据可以是数据库调用、A
Perl 又名实用报表提取语言, 是 Practical Extraction and Report Language 的缩写 Perl 是由 拉里·沃尔(Larry Wall)于19
WSDL 是 Web Services Description Language 的缩写,翻译成中文就是网络服务描述语言 WSDL 是一门基于 XML 的语言,用于描述 Web Services 以
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
我正在寻找解释在 WPF 中创建自定义用户控件的教程。 我想要一个控件,它结合了一个文本 block 、一个文本框和一个启动通用文件打开对话框的按钮。我已经完成了布局,一切都连接好了。它有效,但它是三
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我接近 fourth page of the Django tutorial 的开始看着vote查看,最后是这样的: # Always return an HttpResponseRedirect a
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
是否有任何好的 Qt QSS 教程,或者在某个地方我可以看到样式小部件的示例?如果某处可用,我想要一些完整的引用。除了有关如何设置按钮或某些选项卡样式的小教程外,我找不到任何其他内容。 最佳答案 Qt
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我是一名优秀的程序员,十分优秀!