Q01 SpringBoot 无法进行单元测试
问题
- 在springboot进行单元测试会出现容器找不到,就是容器没有被加载
解决方法
这个问题在pom文件导如单元测试出错,在导入时加入的不需要的配置参数。正确的导入依赖如下:
<!--导入springboot单元测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--导入junit测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
Q02 关于junit导包问题
问题
- 在springboot注解@Test导包时,如果导入的的是junit是不能单独执行某个方法
解决方法
- 导入包:import org.junit.jupiter.api.Test;
Q03 关于Spring Boot数据库配置问题,它自动参数搞出来的错
问题
- 在进行springboot单元测试时出现
Access denied for user ''@'localhost' (using password: NO)
原因
- 主要时因为在Application.yml文件中对数据源的配置出错了。
- 具体看下面代码
解决方法
spring:
datasource:
url: jdbc:mysql://localhost:3306/ligel?characterEncoding=utf8&serverTimezone=UTC
data-username: root
data-password: root
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
spring:
datasource:
url: jdbc:mysql://localhost:3306/ligel?characterEncoding=utf8&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
Q04
问题
如果没有加入mybatis依赖会无法使用mybatis注解的,如@MapperScan
<!--加入mybatis启动类,不然用@MapperScan会报错-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring