[英]Spring: How can I test same class against multiple datasources?
考慮這個例子
@Test
public TestMyProjectIntegration {
@Rule
public JpaRule jpaRule = new JpaRule(H2);
@Test
...
}
localhost
對H2
數據庫運行我的集成測試, staging
// Jenkins中對MySQL
數據庫運行我的集成測試 我最初想過使用Spring Profiles並使用spring.profiles.active=development
和spring.profiles.active=staging
我可以控制,但是
因為我將JpaRule
硬編碼為H2
,所以當spring.profiles.active
更改時我不知道如何將此屬性更改為MySQL
題
在測試期間,春季推薦的指向不同數據庫的方式是什么?
您可以使用系統屬性調用測試,傳遞數據庫詳細信息,如-Dtest.database=H2
並在從jenkins調用測試時更改值
@Rule
public JpaRule jpaRule = new JpaRule(System.getProperty("test.database"));
你的中途....所以在每個配置文件中你需要相同的bean id說dataSource
<beans profiles="dev">
<bean id="dataSource" class="H2"/>
</beans>
<beans profiles="stage">
<bean id="dataSource" class="MySQL"/>
</beans>
然后在你的JpaRule
@Rule
public JpaRule jpaRule = new JpaRule(dataSource);
然后確保在正確的環境中設置正確的spring.active.profile。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.