我有代码来创建数据源,并运行脚本来创建架构并使用基于java的spring配置添加数据。我只想在测试模式下运行这些脚本。我需要在数据库初始值设定项bean上指定要执行此工作的任何注释吗?
最佳答案
您可以为此使用@profile批注,例如:
@Configuration
@Profile("test_profile")
public class StandaloneDataConfig {
@Bean
public DataSource dataSource() {
// do data source creation/initialization
}
}
然后使用:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class,
classes={StandaloneDataConfig.class})
@ActiveProfiles("test_profile")
public class TransferServiceTest {
@Autowired
private TransferService transferService;
@Test
public void testTransferService() {
// test the transferService
}
}
您可以阅读Spring团队的this博客文章,以了解更多详细信息。