【问题标题】:SpringBoot 1.5 : @SpringBootTest and memory DatabaseSpringBoot 1.5:@SpringBootTest 和内存数据库
【发布时间】:2018-11-14 14:10:54
【问题描述】:

我有一个带有 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);
    }
}

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

感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 我没有指定数据库位置的application.yml 文件。我也关闭了我的数据库。我也在控制台中查看:Hibernate: create table connex(id bigint generated by default as ... primary key (id))
    【解决方案2】:

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

    Spring Boot Embedded Database Support

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-30
      • 2021-01-06
      • 2020-07-22
      • 2021-11-27
      • 1970-01-01
      • 2021-09-14
      • 2014-11-06
      • 2019-05-06
      相关资源
      最近更新 更多