一、什么是H2?
H2 數(shù)據(jù)庫是一個用 Java 開發(fā)的嵌入式(內(nèi)存級別)數(shù)據(jù)庫,它本身只是一個類庫,也就是只有一個 jar 文件,可以直接嵌入到項目中。 H2數(shù)據(jù)庫又被稱為內(nèi)存數(shù)據(jù)庫,因為它支持在內(nèi)存中創(chuàng)建數(shù)據(jù)庫。
二、Springboot項目集成H2
為什么測試數(shù)據(jù)庫CRUD
對于Spring項目,特別是測試Service層或者dao層的代碼時。需要驗證訪問數(shù)據(jù)庫的邏輯是否正確。
2.1、引入H2依賴
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
<scope>runtime</scope>
</dependency>
2.2、初始化spring配置文件application.test.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd">
<context:annotation-config/>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<tx:annotation-driven/>
<!-- 初始化h2數(shù)據(jù)庫 -->
<jdbc:embedded-database id="dataSource" generate-name="true" type="H2">
<jdbc:script location="classpath:init/DDL_init.sql"/>
<jdbc:script location="classpath:init/DML_init.sql"/>
</jdbc:embedded-database>
<!-- 創(chuàng)建SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations">
<list>
<value>classpath*:mapper/*.xml</value>
</list>
</property>
</bean>
<!-- 掃描Mapper文件并生成dao對象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.com.jc.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 配置事務(wù)管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean class="cn.com.jc.service.impl.UserServiceImpl"/>
</beans>
2.3、初始化H2數(shù)據(jù)庫DDL/DML語句
DDL_init.sql
CREATE TABLE IF NOT EXISTS`user` (
`id` varchar(255) NOT NULL,
`address` varchar(255) COMMENT '地址',
`created_by` varchar(255) ,
`created_time` datetime(0) ,
`deleted` varchar(255) ,
`last_modified` varchar(255) ,
`last_modified_time` datetime(0) ,
`login_name` varchar(255) COMMENT '登錄名',
`login_password` varchar(255) COMMENT '登錄密碼',
`name` varchar(255) COMMENT '用戶名',
`phone` varchar(255) COMMENT '手機',
`remark` varchar(255) COMMENT '備注',
`user_code` varchar(255) NOT NULL COMMENT '用戶編號',
`user_status` varchar(255) COMMENT '用戶狀態(tài)(狀態(tài)為0顯示可用,狀態(tài)為1顯示停用)',
`email` varchar(255) COMMENT '郵箱',
`profile` varchar(255) COMMENT '登錄密碼',
PRIMARY KEY (`id`)
) ENGINE = InnoDB ;
DML_init.sql
INSERT INTO `user` VALUES ('27bd35f040d74585951974134ec86153', 'test01', 'lxf666', '2021-02-28 12:45:54', '0', 'lxf666', '2021-02-28 13:07:54', 'test01', '$2a$10$QLVCFhfesLhVKezrDd5RsuHqWknndx6XPHnGdXzwjxuoGZWQf2kWK', 'test', 'test', NULL, '0030', '0', 'test@qq.com', '我是test1');
三、編寫單元測試
3.1、首先我們創(chuàng)建測試類
3.2、編寫測試用例
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application-test.xml")
@EnableTransactionManagement
@PowerMockIgnore({"javax.management.*", "javax.script.*"})
public class UserServiceImplTest {
@Autowired
private UserService userService;
@Test
public void selectUserById() {
User user = userService.selectUserById("27bd35f040d74585951974134ec86153");
Assert.assertEquals("27bd35f040d74585951974134ec86153", user.getId());
}
@Test
public void test_update() {
User user = new User();
user.setId("27bd35f040d74585951974134ec86153");
user.setAddress("test01");
user.setProfile("我是test1");
user.setEmail("test@qq.com");
user.setPhone("188888888");
user.setName("test");
userService.update(user);
User newUser = userService.selectUserById("27bd35f040d74585951974134ec86153");
Assert.assertEquals("setPhone", newUser.getPhone());
}
}
引入@PowerMockIgnore({“javax.script.*”})為了執(zhí)行H2初始化腳本,不可缺少
3.3、測試用例
執(zhí)行成功,如果失敗的話需要繼續(xù)調(diào)試,直到成功文章來源:http://www.zghlxwxcb.cn/news/detail-462032.html
注意必須引入@PowerMockIgnore({“javax.management.*”}),不然會報以下這個錯文章來源地址http://www.zghlxwxcb.cn/news/detail-462032.html
到了這里,關(guān)于java單元測試(二)H2數(shù)據(jù)庫篇的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!