如何从测试中排除@EnableJpaRepositories? [英] How to exclude @EnableJpaRepositories from test?

查看:62
本文介绍了如何从测试中排除@EnableJpaRepositories?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a main @SpringBootApplication which needs to scan a specific package in order to enable the JPA repositories, so I use @EnableJpaRepositories to specify that. Right now I'm implementing unit tests and I want to test the Controller component only, so I followed the tutorial in the official docs where they use @WebMvcTest(MyController.class) to test a controller with a service dependency. The problem is that this is not working for me because it is trying to load the JpaRepositories that I specify in the main Spring Boot application (when I comment the @EnableJpaRepositories in the main class the test runs without problem).

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

I'm guessing I need to create a specific configuration for the test class so it can ignore the main configuration (since I only want to load the Controller and mock the service layer), but I don't know how to create such. I tried adding an empty configuration, but it is still failing with the same error:

@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 类:

MyController class:

@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);
  }

}

推荐答案

快速解决方案

从 Spring Boot 应用程序类中删除 @EnableJpaRepositories.使用:

Remove @EnableJpaRepositories from your Spring Boot Application class. Use:

@SpringBootApplication
public class MainApplication { 

}

代替

@SpringBootApplication
@EnableJpaRepositories
public class MainApplication { 

}

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

In this case Spring Boot will find Spring Data JPA on the classpath and uses auto-configuration to scan packages for the repositories.

使用 @EnableJpaRepositories 扫描特定包

Use @EnableJpaRepositories to scan a specific package

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

Use @NikolaiShevchenko solution with a separate configuration, but without explicit importing it (because tests will be explicitly import the configuration too) and let Spring Boot find your configuration during packages scan.

@SpringBootApplication
public class MainApplication { 

}

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

}

重要

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

Don't forget to add basePackages property, if you put your configuration in a separate package.

这篇关于如何从测试中排除@EnableJpaRepositories?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆