簡體   English   中英

Spring:如何針對多個數據源測試同一個類?

[英]Spring: How can I test same class against multiple datasources?

考慮這個例子

@Test
public TestMyProjectIntegration {

  @Rule
  public JpaRule jpaRule = new JpaRule(H2);

  @Test
  ...
}
  • 我想在localhostH2數據庫運行我的集成測試,
  • 我想在staging // Jenkins中對MySQL數據庫運行我的集成測試

我最初想過使用Spring Profiles並使用spring.profiles.active=developmentspring.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM