SpringBoot 1.5:@SpringBootTest和内存数据库

时间:2018-06-05 07:12:26

标签: spring-boot spring-data-jpa spring-boot-test

我有一个带有spring data / jpa的SpringBoot应用程序来连接数据库  还有一个属性文件yml,用于定义数据库连接。

一切都很顺利。

我创建了一个类似的测试:

@ActiveProfiles("dev")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyMicroServiceApp.class, webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MyMicroServiceAppTest {

@Test
public <T> void postConnex() {
    //Create Object connexCreate
    ...

    // Create POST
    ResponseEntity<Udsaconnex> result1 = this.restTemplate().postForEntity("http://localhost:" + port + "/v1/connex",
            connexCreate, Udsaconnex.class);
    id = result1.getBody().getIdconnex();
    assertEquals(result1.getBody().toString().isEmpty(), false);
    }
}

对于我的测试,我没有配置数据库连接的属性,但测试工作和我在控制台中查看: Hibernate:如果存在,则删除表连接 我不明白为什么, @SpringBootTest 自动模拟数据库如 @DataJpaTest 这是可能的,但我在春季启动文档中找不到任何相关内容。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

如果您有application.yml文件指定数据库位置,那么SpringBootTest显然会使用相同的配置,并将使用您配置的数据库。

答案 1 :(得分:0)

从你问题的标题我猜你的构建依赖项中有一个内存数据库。如果在类路径中找到它们,则Spring-boot会对某些数据库(H2,HSQL,Derby)进行一些自动配置。请参阅此链接以获取支持的数据库列表:

Spring Boot Embedded Database Support