源码下载地址: 链接: https://pan.baidu.com/s/1v5FmbEvWtQXmv1DGnIOPvw 提取码: cxtn
CSDN下载: https://download.csdn.net/download/zcf980/10719897
在网上找了很久都没找到关于SpringBoot多模块的有效单元测试方法, 后来自己摸索了很久才解决, 写两篇博客来帮助大家
此篇为 Dao 层的测试代码 Service测试请参考: https://blog.csdn.net/zcf980/article/details/83118300
本项目 分一个父工程 demo 和 三个子模块(demo-dao, demo-service, demo-web), 一个数据库 test , test里只有一张demo 表: 生成demo表的代码:
CREATE TABLE `demo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
贴上demo-dao层的目录结构, 很简单:
1. Demo.java, DemoRepository.java;
2. DemoTest.java, Dao层测试启动类DemoDaoApplicationTests.java,
3. pom.xml, application.yml
!!!!!!注意: 配置文件 application.yml 必须放在 demo-dao 的 Resource 目录下, 放在demo-service或demo-web目录下的话, 测试 Dao 层的时候配置文件不会扫描到, 因此会报错
默认的测试启动类是这样的:
package demo.demodao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoDaoApplicationTests {
@Test
public void contextLoads() {
}
}
使用这个默认测试启动类的话, 汇报找不到 Bean 的错, 网上说了很多修改 启动类的扫描路径的方法, 经过测试都不行
解决方案:
修改这个类,是他变成一个普通的启动类, 这样根据 SpringBoot 扫描包的规则: 自启动类所在包自上而下扫描,
便可扫描到 DemoRepository 这个 Bean
修改后的 DemoDaoApplicationTests.java:
package demo.demodao;
import org.junit.Test;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoDaoApplicationTests {
@Test
public void contextLoads() {
}
}
Demorepository.java在 demo .demodao 包下
DemoDaoApplicationTests.java 也在 demo.demodao 包下
运行测试代码, 测试成功!!!!!!!!!
下面贴上其他几个文件的代码:
DemoTest.java
package demo.demodao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoTest {
@Autowired
private DemoRepository demoRepository;
@Test
public void testAdd(){
Demo demo = new Demo();
demo.setId(1);
demo.setName("TestName");
Demo result = this.demoRepository.save(demo);
System.out.println(result == null? "插入成功" : "插入失败");
}
}
Demo.java
package demo.demodao;
import javax.persistence.*;
@Entity(name = "demo") //设置实体名, 在数据库中是表名
public class Demo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO) //设置自动增长
@Column(name = "id")
private Integer id;
@Column(name = "cname") //设置数据库字段名
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
DemoRepository.java
package demo.demodao;
import org.springframework.data.jpa.repository.JpaRepository;
public interface DemoRepository extends JpaRepository<Demo, Integer> {
}
application.xml
spring:
datasource:
# jdbc:mysql://localhost:3306/test 数据库地址
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root # 数据库用户名 修改为自己数据库用户名和密码
password: xxxx # 数据库密码
driver-class-name: com.mysql.jdbc.Driver # 数据库驱动
jpa:
hibernate:
ddl-auto: none # create-drop 如果实体对应的表已存在,先删除再创建,否则直接创建
# !!!注意: 第一次运行时可设置为 create-drop, 这样就不需要手动创建数据库表, 但是后面运行务必设置为none
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>demo</groupId>
<artifactId>demo-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo-dao</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- 添加 demo-base 的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>