SpringBoot 多模組 Dao 層測試
原始碼下載地址:
在網上找了很久都沒找到關於SpringBoot多模組的有效單元測試方法, 後來自己摸索了很久才解決, 寫兩篇部落格來幫助大家
此篇為 Dao 層的測試程式碼
本專案 分一個父工程 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層的目錄結構, 很簡單:
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.demoservice; 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
修改後的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.demo .demodao 包下
DemoDaoApplicationTests.java 也在 demo.demodao 包下
執行測試程式碼, 測試成功!!!!!!!!!
下面貼上其他幾個檔案的程式碼:
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> {
}
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? "插入成功" : "插入失敗");
}
}
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>