源碼下載下傳位址: 連結: 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>