2 回答

TA貢獻(xiàn)1801條經(jīng)驗 獲得超8個贊
我能夠通過在我的測試類上使用以下注釋將內(nèi)存數(shù)據(jù)庫添加到 Spring 上下文:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { JpaRepositoriesConfig.class })
@DataJpaTest
當(dāng)然,包括在我的 pom 中必要的依賴項:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
如果我正在做的事情違反了任何最佳實踐,請告訴我。

TA貢獻(xiàn)1810條經(jīng)驗 獲得超5個贊
我確實看到您正在維護(hù)單獨的屬性文件以進(jìn)行測試,這意味著您正在使用測試屬性創(chuàng)建數(shù)據(jù)源和實體管理器并加載 spring boot 原始應(yīng)用程序上下文。所以你不需要任何額外的測試配置
@RunWith(SpringRunner.class)
@SpringBootTest
public class JpaEntityTest {
@Test
public void test(){}
}
您還可以使用配置文件來執(zhí)行此操作,命名application.properties并application-test.properties使用@Profile和@ActiveProfile
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfile("test")
@Profile("test")
public class JpaEntityTest {
@Test
public void test(){}
}
添加回答
舉報