【问题标题】:How to exclude @EnableJpaRepositories from test?如何从测试中排除@EnableJpaRepositories?
【发布时间】:2021-02-25 00:08:33
【问题描述】:

我有一个主要的@SpringBootApplication,它需要扫描特定的包以启用 JPA 存储库,所以我使用 @EnableJpaRepositories 来指定它。现在我正在实现单元测试并且我只想测试控制器组件,所以我按照官方文档中的教程使用@WebMvcTest(MyController.class) 来测试具有服务依赖关系的控制器。 问题是这对我不起作用,因为它正在尝试加载我在主 Spring Boot 应用程序中指定的 JpaRepositories(当我在主类中注释 @EnableJpaRepositories 时,测试运行没有问题)。

我猜我需要为测试类创建一个特定的配置,以便它可以忽略主要配置(因为我只想加载控制器并模拟服务层),但我不知道如何创建这样的。我尝试添加一个空配置,但仍然失败并出现同样的错误:

@TestConfiguration
static class TestConfig {}

这是我得到的错误:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'failureTaskHandler': Unsatisfied dependency expressed through field 'myManager'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'msgManager': Unsatisfied dependency expressed through field 'inboundManager'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'inboundManager': Unsatisfied dependency expressed through field 'messageRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageRepository' defined in com.example.MessageRepository defined in @EnableJpaRepositories declared on MonitorApplication: Cannot create inner bean '(inner bean)#45e639ee' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#45e639ee': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

还有我的测试课:

@WebMvcTest(MyController.class)
public class MyControllerTest {

  @Autowired private MockMvc mvc;

  @MockBean private MyService service;


  // Tests here

  // @Test
  // public void...


}

MyController 类:

@RestController
@CrossOrigin
@RequestMapping("/api")
@Slf4j
public class MyController {

  @Autowired private MyService service;

  @PostMapping(value = "/search", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody SearchResponse getOrders(@RequestBody SearchRequest orderSearchRequest) {
    log.info("Receiving orders request...");
    return service.getOrders(orderSearchRequest);
  }

}

【问题讨论】:

  • 能不能显示MyController的代码
  • 好的,我会把它添加到原帖中
  • myManager 在哪里使用?
  • myManager 是我在主应用程序的其他部分使用的一些外部库的一部分,但我想忽略所有这些,因为我将模拟服务响应,因为我正在对控制器进行单元测试。 myManager 不被存储库或服务使用,只是我的应用程序的另一个功能。

标签: java spring-boot unit-testing spring-data spring-mvc-test


【解决方案1】:

快速解决方案

从您的 Spring Boot 应用程序类中删除 @EnableJpaRepositories。使用:

@SpringBootApplication
public class MainApplication { 

}

代替

@SpringBootApplication
@EnableJpaRepositories
public class MainApplication { 

}

在这种情况下,Spring Boot 将在类路径中找到 Spring Data JPA,并使用自动配置来扫描存储库的包。

使用@EnableJpaRepositories 扫描特定包

使用带有单独配置的@NikolaiShevchenko 解决方案,但没有显式导入它(因为测试也会显式导入配置)并让 Spring Boot 在包扫描期间找到您的配置。

@SpringBootApplication
public class MainApplication { 

}

@Configuration
@EnableJpaRepositories(basePackages = "com.app.entities")
public class JpaConfig {

}

重要

如果您将配置放在单独的包中,请不要忘记添加 basePackages 属性。

【讨论】:

    【解决方案2】:

    声明单独的配置

    @Configuration
    @EnableJpaRepositories
    public class DataConfiguration { ... }
    

    将其导入应用程序

    @SpringBootApplication
    @Import({ DataConfiguration.class })
    public class MainApplication { ... } 
    

    但不要导入到MyControllerTest

    【讨论】:

    • 主应用程序运行良好,但测试类仍然失败并出现同样的错误
    • @ErickSiordia 删除这个@Import({ DataConfiguration.class })
    猜你喜欢
    • 2017-07-26
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 2017-07-23
    • 2018-07-06
    • 2013-01-20
    相关资源
    最近更新 更多