【单元测试】Spring Data JPA + H2 测试DAO层

打印 上一主题 下一主题

主题 910|帖子 910|积分 2730

1. 环境配置


  • Springboot 2.7.8
  • h2 2.1.214
2. POM文件


  • 引入springboot parent pom
点击查看代码
  1. <parent>
  2.   <groupId>org.springframework.boot</groupId>
  3.   <artifactId>spring-boot-starter-parent</artifactId>
  4.   <version>2.7.8</version>
  5.   <relativePath/>
  6. </parent>
复制代码

  • 引入junit , springboot-test, spring-data-jpa,  H2
点击查看代码
  1.         <dependency>
  2.             <groupId>org.springframework.boot</groupId>
  3.             <artifactId>spring-boot-starter-test</artifactId>
  4.             <scope>test</scope>
  5.             <exclusions>
  6.                 <exclusion>
  7.                     <groupId>org.junit.vintage</groupId>
  8.                     <artifactId>junit-vintage-engine</artifactId>
  9.                 </exclusion>
  10.             </exclusions>
  11.         </dependency>
  12.         <dependency>
  13.                 <groupId>org.springframework.boot</groupId>
  14.                 <artifactId>spring-boot-starter-data-jpa</artifactId>
  15.         </dependency>
  16.        
  17.         <dependency>
  18.             <groupId>junit</groupId>
  19.             <artifactId>junit</artifactId>
  20.             <scope>test</scope>
  21.         </dependency>
  22.        
  23.         <dependency>
  24.             <groupId>com.h2database</groupId>
  25.             <artifactId>h2</artifactId>
  26.             <scope>test</scope>
  27.         </dependency>
复制代码
3. H2配置

H2是一款内存数据库,可以配合单测测试数据层。实现原理是将依赖的三方数据库通过内存的方式进行mock。其中H2数据库的数据表结构以及数据可以通过sql脚本文件初始化,也可以在代码中增删数据。
test/resources/h2conf.properties
  1. #使用h2数据库
  2. spring.datasource.url=jdbc:h2:mem:test;MODE=Oracle;
  3. spring.datasource.username=sa
  4. spring.datasource.password=
  5. spring.datasource.driver-class-name=org.h2.Driver
  6. spring.sql.init.platform=h2
  7. #数据初始化脚本地址
  8. spring.sql.init.schema-locations=classpath:script/schema-h2.sql
  9. # jpa配置
  10. spring.jpa.database=h2
  11. spring.jpa.show-sql=true
  12. spring.jpa.hibernate.ddl-auto=none
  13. spring.jpa.generate-ddl=false
  14. spring.jpa.open-in-view=true
  15. spring.main.banner-mode=off
复制代码
在 test/resources/script 下创建schema-h2.sql,内容为建表语句。
4. 编写单测代码

使用 @DataJpaTest 测试spring-data-jpa 框架下的数据层,@TestPropertySource 引入指定配置文件, @ContextConfiguration 引入springboot上下文。
点击查看代码
  1. @DataJpaTest
  2. @ContextConfiguration(classes=Application.class)
  3. @TestPropertySource(locations = {"classpath:h2conf.properties"})
  4. public class ServiceImplDaoTest {
  5.     private SomeServiceImpl service;
  6.     @Autowired
  7.     private SomeRepository repository;
  8.     @Autowired
  9.     private TestEntityManager entityManager;
  10.     @BeforeEach
  11.     public  void setUp() {
  12.         service = new SomeServiceImpl();
  13.         ReflectionTestUtils.setField(service, "repository", repository);
  14.     }
  15.     @Test
  16.     public void testSelect() {
  17.         insertData();
  18.         List<SomeEntity> all =         service.findAll();
  19.         Assertions.assertEquals(10, all.size());
  20.     }
  21.     /**
  22.      * for test sql
  23.      */
  24.     public void insertData() {
  25.         // mock 10 data
  26.         for (int i = 0; i < 10; i++) {
  27.             SomeEntity entity = new SomeEntity();
  28.             entityManager.persist(entity);
  29.         }
  30.     }
  31. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

慢吞云雾缓吐愁

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表