H2 数据库是一个用 Java 开发的嵌入式(内存级别)数据库,它本身只是一个类库,也就是只有一个 jar 文件,可以直接嵌入到项目中。 H2数据库又被称为内存数据库,因为它支持在内存中创建数据库。
为什么测试数据库CRUD
对于Spring项目,特别是测试Service层或者dao层的代码时。需要验证访问数据库的逻辑是否正确。
<dependency>
<groupId>com.h2databasegroupId>
<artifactId>h2artifactId>
<version>1.4.193version>
<scope>runtimescope>
dependency>
<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/>
<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>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="mapperLocations">
<list>
<value>classpath*:mapper/*.xmlvalue>
list>
property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.com.jc.mapper">property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
bean>
<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>
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 '用户状态(状态为0显示可用,状态为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', '[email protected]', '我是test1');
@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("[email protected]");
user.setPhone("188888888");
user.setName("test");
userService.update(user);
User newUser = userService.selectUserById("27bd35f040d74585951974134ec86153");
Assert.assertEquals("setPhone", newUser.getPhone());
}
}
引入@PowerMockIgnore({“javax.script.*”})为了执行H2初始化脚本,不可缺少