在 springboot 中进行单独的 mybatis 单元测试

在 springboot 中进行单独的 mybatis 单元测试,第1张

使用普通的@SpringBootTest进行单元测试时会将整个应用都启动,和正常启动工程没什么区别。非常耗时。

如下,启动测试。将web层也启动了。事实上根本不需要启动这个。

我们只需要启动dao就行了。在这里我们使用mybatis-spring-boot-starter-test这个依赖

测试例子厅毁御

打印扮岩sql

1、java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

需要和springboot main方法放到同一级包下

2、Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exceptionnested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

若依工程引入这个真的有点难,推荐还是使用@SpringBootTest的方式

普通单数据源请配置如下:spring.datasource.url的形式;否则单元测试启动报错余此'url' attribute is not specified and no embedded datasource could be configured.

有多个数据源时才去配置这样,spring.datasource.druid.master.url;

1. 工程目录结构整理清楚

在src/main/java文件夹中,新建包cn.springmvc.model(存放javabean),

cn.springmvc.dao(存放spring与mybatis连接接口),

cn.springmvc.service(大橡service接口),

cn.springmvc.service.impl(service接口的实现),

cn.springmvc.controller(存放控制层controller)

在src/main/resource文件夹中,新建包conf(存放配置文件),

mapper(mybatis的mapper文件)

在src/test/java文件夹中,新建包cn.springmvc.test(存放测试文件)

在WEB-INF文件夹下新建jsp文件夹(存放jsp文件)

这样项目结构基本完成了

2. 引入依赖包

打开maven的pom文件,对本次开发所需使用的架包依次导入(maven项目管理的优没大势)

查找依赖结构有个不错的网站,http://search.maven.org/ 只要输入包名即可查找引来关系

pom.xml(包依赖)

?

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

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>eyas.springmvc</groupId>

<artifactId>springmvc</artifactId>

<packaging>war</packaging>

<version>0.0.1-SNAPSHOT</version>

<name>springmvc

Maven Webapp</name>

<url>http://maven.apache.org</url>

<properties>

<!--

spring版本号 -->

<spring.version>3.2.4.RELEASE</spring.version>

<!--

mybatis版本号 -->

<mybatis.version>3.2.4</mybatis.version>

<枯仿竖!--

log4j日志文件管理包版本 -->

<slf4j.version>1.6.6</slf4j.version>

<log4j.version>1.2.9</log4j.version>

</properties>

<dependencies>

<!--

spring核心包 -->

<!--

springframe start -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-oxm</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aop</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aop</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>${spring.version}</version>

</dependency>

<!--

springframe end -->

<!--

mybatis核心包 -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>${mybatis.version}</version>

</dependency>

<!--

mybatis/spring包 -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>1.2.2</version>

</dependency>

<!--

mysql驱动包 -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.29</version>

</dependency>

<!--

junit测试包 -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.11</version>

<scope>test</scope>

</dependency>

<!--

阿里巴巴数据源包 -->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid</artifactId>

<version>1.0.2</version>

</dependency>

<!--

json数据 -->

<dependency>

<groupId>org.codehaus.jackson</groupId>

<artifactId>jackson-mapper-asl</artifactId>

<version>1.9.13</version>

</dependency>

<!--

日志文件管理包 -->

<!--

log start -->

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>${log4j.version}</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>${slf4j.version}</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>${slf4j.version}</version>

</dependency>

<!--

log end -->

</dependencies>

<build>

<finalName>springmvc</finalName>

</build>

</project>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12401524.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-23
下一篇2023-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存