java - 如何从测试中排除@EnableJpaRepositories?

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

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

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

@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...


}

我的 Controller 类:

@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。使用:

@SpringBootApplication
public class MainApplication { 

}

代替

@SpringBootApplication
@EnableJpaRepositories
public class MainApplication { 

}

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

使用@EnableJpaRepositories扫描特定包

通过@Import({ DataConfiguration.class }) 使用@NikolaiShevchenko 解决方案(这是不正确的)和单独的配置,但没有显式导入它,(因为测试将显式导入配置也是)并让 Spring Boot 在包扫描期间找到您的配置。

@SpringBootApplication
public class MainApplication { 

}

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

}

重要

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

关于java - 如何从测试中排除@EnableJpaRepositories?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62517000/

相关文章:

java - 我的简单 antlr 语法有什么问题?

java - 使用 Eclipse 作为服务器调试远程 Java 应用程序(套接字监听)

Java 堆栈溢出错误

java - 使用数组

Spring 启动: Repository interface sql injection protection

java - Maven 找不到套件测试?

spring - 使用 Hibernate L2 ehcache 和 spring bootenabledCache 时已经存在另一个未命名的 CacheManager

java - 如何在spring boot app中使用spring transaction配置kafka transaction manager

.net - 无法排队测试运行 : Unable to start the agent process(Visual Studio 2010)

XCode 4 单元测试 : Is it possible to ignore certain test cases?