我只是想为访问MySQL数据库的服务添加测试用例,我想重新创建整个模式(对于某些场景,也只是使用带有每个测试用例所需数据的MySQL转储文件).我环顾四周,发现有些人使用SQLite/H2和其他人来做这件事,但我只是在游荡,如果有任何办法在内存中运行MySQL,所以我不需要担心MySQL的任何特定内容方言我可能正在使用我们的服务.
如何配置我的Spring Boot应用程序,以便在运行单元测试时,它将使用内存数据库,如H2/HSQL,但是当我运行Spring Boot应用程序时,它将使用生产数据库[Postgre/MySQL]?
spring spring-test spring-data spring-test-dbunit spring-boot
我正在使用内存数据库(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
}
}
Run Code Online (Sandbox Code Playgroud)
服务实现应使用保存在内存数据库中的数据,并且很少执行业务验证。我在这里面临的问题是服务实现实际上是在考虑ManufacturerRepository实例,该实例指向实际的db(在这种情况下为postgres),而不是指向内存数据库。关于如何将指示厂商数据库指向内存数据库的ManufacturerRepository实例注入厂商serviceImpl服务实现的任何帮助