2 回答
TA贡献1801条经验 获得超8个赞
我能够通过在我的测试类上使用以下注释将内存数据库添加到 Spring 上下文:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { JpaRepositoriesConfig.class })
@DataJpaTest
当然,包括在我的 pom 中必要的依赖项:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
如果我正在做的事情违反了任何最佳实践,请告诉我。
TA贡献1810条经验 获得超5个赞
我确实看到您正在维护单独的属性文件以进行测试,这意味着您正在使用测试属性创建数据源和实体管理器并加载 spring boot 原始应用程序上下文。所以你不需要任何额外的测试配置
@RunWith(SpringRunner.class)
@SpringBootTest
public class JpaEntityTest {
@Test
public void test(){}
}
您还可以使用配置文件来执行此操作,命名application.properties并application-test.properties使用@Profile和@ActiveProfile
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfile("test")
@Profile("test")
public class JpaEntityTest {
@Test
public void test(){}
}
添加回答
举报