目錄
省流:
正文
一、直接測(無需配置掃描和xml)
1. 場景
2. 無需配置掃描,直接在測試類注入Mapper
3. 報錯
補充:
關(guān)于@RunWith
常見的報錯:
1.包名不同導(dǎo)致報錯
省流:
test目錄下配置文件:application.yml中配置數(shù)據(jù)庫信息。
test目錄下各類:直接寫測試類即可,直接調(diào)用main目錄下各Mapper類和實體類即可。
測試類:?
@RunWith(SpringRunner.class)
@MybatisPlusTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Rollback(false)
public class Test1 {
@Autowired
private T1Mapper t1Mapper;
@Test
public void t1() {
t1Mapper.selectOne();
}
}
application.yml配置文件:?
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
# 生產(chǎn)
url: jdbc:mysql://ip:port/xxx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
username: root
password: root
# 初始連接數(shù)
initialSize: 5
# 最小連接池數(shù)量
minIdle: 10
# 最大連接池數(shù)量
maxActive: 20
# 配置獲取連接等待超時的時間
maxWait: 60000
# 配置間隔多久才進行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
minEvictableIdleTimeMillis: 300000
# 配置一個連接在池中最大生存的時間,單位是毫秒
maxEvictableIdleTimeMillis: 900000
# 配置檢測連接是否有效
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
webStatFilter:
enabled: true
statViewServlet:
enabled: true
# 設(shè)置白名單,不填則允許所有訪問
allow:
url-pattern: /druid/*
# 控制臺管理用戶名和密碼
login-username:
login-password:
filter:
stat:
enabled: true
# 慢SQL記錄
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true
mybatis-plus:
# mapper-locations: classpath:/mapper/*Mapper.xml
# typeAliasesPackage: com.ali.entity
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
目錄結(jié)構(gòu):
目錄結(jié)構(gòu)
src
--main
--test
? -- java
? ? ?-- package和java文件
? -- resources
? ? ?-- application.yml
正文
一、直接測(無需配置掃描和xml)
1. 場景
在公司項目里,有時候想測下某條sql(不想調(diào)整個接口),有時想直接調(diào)用測試環(huán)境的數(shù)據(jù)庫,或者生產(chǎn)環(huán)境的數(shù)據(jù)庫。就只需單獨使用mybatis測,無需啟動整個springboot。
2. 無需配置掃描,直接在測試類注入Mapper
無需配置掃描等。
無需如下配置:
啟動類加注解:
@MapperScan("com.cloud.xxx.mapper")
配置文件:mybatis-plus.mapper-locations=classpath:com/cloud/xxx/mapper/xml/*.xml
注意:
直接調(diào)用mybatis,如果test目錄中存在別人寫的其他測試類,尤其是有些測試類用springboot啟動的,會影響到你測試類啟動,可能會報如下錯誤。你不需要去解決這些錯,而是直接把其他人的測試類干掉,你的測試類就能啟動成功了。
3. 報錯
報錯1:?
No qualifying bean of type 'com.xxx.mapper.xxxMapper' available:
expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
?這個報錯一般是指沒有Mapper,通常是沒有掃描到,需要加?@MapperScan
報錯2:?
Annotation-specified bean name 'xxxMapper' for bean class conflicts with existing,
?這個報錯是存在多個相同名字的Mapper。
補充:
關(guān)于@RunWith
@RunWith:就是一個運行器
@RunWith(JUnit4.class) 指用JUnit4測試工具來運行測試。
@RunWith(SpringJUnit4ClassRunner.class):指讓類運行在Spring的測試環(huán)境,以便測試開始時自動創(chuàng)建Spring應(yīng)用上下文,并使用JUnit4測試工具運行測試。
@RunWith(SpringRunner.class):SpringRunner繼承了 SpringJUnit4ClassRunner ,所以等價于@RunWith(SpringJUnit4ClassRunner.class),為了名字簡短,方便使用
@RunWith(Suite.class):一套測試集合(suite指一套,使用RunWith測試套件)
通常,使用 @RunWith(SpringRunner.class)。
常見的報錯:
1.包名不同導(dǎo)致報錯
報錯 Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration (SpringBoot測試)_globalcoding的博客-CSDN博客
參考
Spring boot Mybatis-Plus數(shù)據(jù)庫單測實戰(zhàn)(三種方式)_@mybatisplustest_CuteXiaoKe的博客-CSDN博客
在 springboot 中進行單獨的 mybatis 單元測試 - 簡書
MyBatis plus和maven的依賴_mybatisplus maven依賴_阿飛0x6c717a的博客-CSDN博客
測試類的@RunWith與@SpringBootTest注解_Morning sunshine的博客-CSDN博客
======================分割線=======================?
文章到此已經(jīng)結(jié)束,以下是紫薯布丁
@RunWith(SpringRunner.class)
@MybatisPlusTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Rollback(false)
public class Test1 {
? ? @Autowired
? ? private T1Mapper t1Mapper;
? ? @Test
? ? public void t1() {
? ? ? ? t1Mapper.selectOne();
? ? }
}
spring:
? datasource:
? ? type: com.alibaba.druid.pool.DruidDataSource
? ? driverClassName: com.mysql.cj.jdbc.Driver
? ? # 生產(chǎn)
? ? url: jdbc:mysql://ip:port/xxx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
? ? username: root
? ? password: root
? ? # 初始連接數(shù)
? ? initialSize: 5
? ? # 最小連接池數(shù)量
? ? minIdle: 10
? ? # 最大連接池數(shù)量
? ? maxActive: 20
? ? # 配置獲取連接等待超時的時間
? ? maxWait: 60000
? ? # 配置間隔多久才進行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
? ? timeBetweenEvictionRunsMillis: 60000
? ? # 配置一個連接在池中最小生存的時間,單位是毫秒
? ? minEvictableIdleTimeMillis: 300000
? ? # 配置一個連接在池中最大生存的時間,單位是毫秒
? ? maxEvictableIdleTimeMillis: 900000
? ? # 配置檢測連接是否有效
? ? validationQuery: SELECT 1 FROM DUAL
? ? testWhileIdle: true
? ? testOnBorrow: false
? ? testOnReturn: false
? ? webStatFilter:
? ? ? enabled: true
? ? statViewServlet:
? ? ? enabled: true
? ? ? # 設(shè)置白名單,不填則允許所有訪問
? ? ? allow:
? ? ? url-pattern: /druid/*
? ? ? # 控制臺管理用戶名和密碼
? ? ? login-username:
? ? ? login-password:
? ? filter:
? ? ? stat:
? ? ? ? enabled: true
? ? ? ? # 慢SQL記錄
? ? ? ? log-slow-sql: true
? ? ? ? slow-sql-millis: 1000
? ? ? ? merge-sql: true
? ? ? wall:
? ? ? ? config:
? ? ? ? ? multi-statement-allow: true
mybatis-plus:
# ?mapper-locations: classpath:/mapper/*Mapper.xml
# ?typeAliasesPackage: com.ali.entity
? configuration:
? ? log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
test目錄結(jié)構(gòu)
src
--main
--test
? -- java
? ? ?-- package和java文件
? -- resources
? ? ?-- application.yml
No qualifying bean of type 'com.xxx.mapper.xxxMapper' available:?
expected at least 1 bean which qualifies as autowire candidate.?
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}文章來源:http://www.zghlxwxcb.cn/news/detail-469570.html
Annotation-specified bean name 'xxxMapper' for bean class conflicts with existing,
?文章來源地址http://www.zghlxwxcb.cn/news/detail-469570.html
到了這里,關(guān)于junit單元測試 mybatis 單獨測試 springboot的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!