![](/img/trans.png)
[英]In-memory database configuration (HSQLDB) for integration testing in spring boot app
[英]Integration test in Spring Boot using in-memory database
我正在使用内存数据库(H2)进行集成测试,以便我可以使用已知值填充存储库,使用存储库初始化服务实现。 这是我的测试课
@RunWith(SpringRunner.class)
@TestPropertySource("classpath:application-test.properties")
@SpringBootTest
public class ManufacturerServiceH2ImplTest {
@Autowired
private ManufacturerRepository manufacturerRepository;
@Autowired
ManufacturerServiceImpl manufacturerServiceImpl;
@Test
public void testManufacturerCreate() throws Exception {
//Create Manufacturer
Manufacturer manufacturer = new Manufacturer();
manufacturer.setManufacturerId("SSS");
manufacturer.setManufacturerName("WWW");
//Save Manufacturer in Inmemory
Manufacturer manufacturerInMemory = manufacturerRepository.save(manufacturer);
//Service Implementation
StResponse createManufacturer = manufacturerServiceImpl.createManufacturer(manufacturer);
//Compare the result
}
}
服务实现应该使用保存在内存数据库中的数据并执行少量业务验证。 我在这里面临的问题是,服务实现实际上正在考虑指向实际 db(在本例中为 postgres)而不是指向内存数据库的制造商存储库实例。 有关如何将制造商存储库实例注入制造商服务Impl 服务实现的任何帮助,该服务实现指向内存数据库
当集成测试运行时使用 Spring-Profiles 使用 H2,否则使用另一个数据库。
将application-test.{yml|properties}
到资源并将@ActiveProfiles("test")
到您的类。
应用程序-test.yml
spring.profiles.active: test
spring:
jpa:
database: h2
datasource:
url: jdbc:h2:mem:AZ
driver-class-name: org.h2.Driver
h2:
console:
enabled: true
如果您想使用首选数据库(testContainer - Docker)进行集成测试,请在此处查看答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.