Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java spring boot集成测试-数据库未与@WebMvcTest自动连接_Java_Spring_Jpa_Spring Boot_Integration Testing - Fatal编程技术网

Java spring boot集成测试-数据库未与@WebMvcTest自动连接

Java spring boot集成测试-数据库未与@WebMvcTest自动连接,java,spring,jpa,spring-boot,integration-testing,Java,Spring,Jpa,Spring Boot,Integration Testing,我有一个sprig引导(版本1.5.6)应用程序,它使用以下内容: SpringBootStarter数据jpa(jpa存储库和实体) spring boot starter数据(无存储库或实体) spring boot starter数据休息(用于HAL支持) 现在,我正在为这个应用程序创建单元测试。在一个测试用例中,我有以下注释: @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RA

我有一个sprig引导(版本1.5.6)应用程序,它使用以下内容:

  • SpringBootStarter数据jpa(jpa存储库和实体)
  • spring boot starter数据(无存储库或实体)
  • spring boot starter数据休息(用于HAL支持)
现在,我正在为这个应用程序创建单元测试。在一个测试用例中,我有以下注释:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "spring.cloud.enabled=false" })
@RunWith(SpringRunner.class)
@WebMvcTest(MyRestController.class)
该测试正确地初始化了jpa存储库,我能够对其进行测试

然后,我有另一个带有以下注释的测试:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "spring.cloud.enabled=false" })
@RunWith(SpringRunner.class)
@WebMvcTest(MyRestController.class)
此测试设置Mockmvc,但不初始化JPA存储库。它只初始化配置的MVC部分。但是我也需要初始化JPA存储库。我用data.sql文件设置了测试数据,该文件作为内存H2数据库加载。我得到的错误是:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
我尝试了许多没有成功的事情:

  • 使用这两个注释不起作用,因为类只能有一个“BootstrapWith”注释
  • @EnableJParepositions不起作用
  • @自动配置测试数据库不工作
在进行上下文初始化时,我确实看到了以下内容:

.s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
现在,由于spring能够在第一次测试中自动连接jpa存储库,并且在应用程序中运行良好,因此我认为它也应该能够在webMvc测试用例中自动连接存储库

我可以在测试包中创建一个配置文件并初始化实体管理器、数据源等,但如果有一种方法可以使用spring自动连接,那么我不想管理该配置


请建议。

我看到您有
@WebMvcTest
注释。这个特定的测试只测试web层,它不加载整个应用程序上下文,只加载web上下文。您可能需要切换到
@SpringBootTest
@AutoConfigureMockMvc
来测试整个堆栈

使用Spring Boot进行JPA测试的方法是使用
@DataJpaTest
注释。它会自动配置所有内容,前提是类路径中有内存中的DB(如果使用maven,请确保它在“测试”范围内)。它还提供了
TestEntityManager
,它是JPA的
EntityManager
接口的实现,具有一些有用的测试功能

例如:

@RunWith(SpringRunner.class)
@DataJpaTest
pubic class EntityTest {
    @Autowired TestEntityManager entityManager;

    @Test
    public void saveShouldPersistData() throws Exception {
        User saved = entityManager.persistFlushFind(new User("username", "password"));
        assertNonNull(saved);
    }
}
在pom.xml中,您可以添加H2数据库(Spring Boot还可以自动配置Derby和HSQLDB)


com.h2数据库
氢
测试

我知道这个问题已经得到了回答,但只需增加几分钱,您就可以使用@contextConfiguration加载与数据库相关的和其他spring配置,它可以包含xml和类配置文件。如果您使用gradle,请在
依赖项中使用
testCompile('com.h2数据库:h2')