【发布时间】:2019-08-31 14:25:44
【问题描述】:
我想用内存中的 MongoDB 数据库创建一个 JUnit 5 控制器集成测试。测试运行后出现错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
A component required a bean named 'mongoTemplate' that could not be found.
Action:
Consider defining a bean named 'mongoTemplate' in your configuration.
我的控制器看起来像:
@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = DeviceController.class)
@EnableMongoRepositories
class DeviceControllerTest {
@MockBean
private MongoTemplate mongoTemplate;
@Autowired
private MockMvc mockMvc;
@MockBean
private DeviceService deviceService;
}
我试着说:
excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class
或:
@ContextConfiguration(classes = {SpringMongoConfiguration.class})
配置类如:
@EnableMongoRepositories
@EnableAutoConfiguration
public class SpringMongoConfiguration {
@Bean
public MongoTemplate mongoTemplate() throws Exception {
return new MongoTemplate(new MongoClient("localhost"), "findall");
}
}
我的 pom.xml 看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.interview</groupId>
<artifactId>exercise</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>device</name>
<properties>
<java.version>11</java.version>
<hibernate.version>5.4.0.Final</hibernate.version>
<mapstruct.processor.version>1.3.0.Final</mapstruct.processor.version>
<mapstruct.version>1.3.0.Final</mapstruct.version>
<spring.cloud.starter.netflix.hystrix.version>2.1.2.RELEASE</spring.cloud.starter.netflix.hystrix.version>
<apache.common.lang.version>3.0</apache.common.lang.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-mongodb</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache.common.lang.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>${spring.cloud.starter.netflix.hystrix.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
<configuration>
<source>11</source>
<target>11</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.processor.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>spring-boot-device-docker</finalName>
</build>
</project>
我认为我检查了所有可能的选项,但真的不知道接下来要做什么才能达到目标。对于如何为 JUnit 5 控制器测试配置我的测试类的建议,我将不胜感激。
【问题讨论】:
-
这个问题令人困惑 - 一方面您想使用内存中的 Mongo 实例,另一方面 - 您正在模拟服务(和 MongoTemplate)。请注意,
@WebMvcTest将禁用完全自动配置,而是仅应用与 MVC 测试相关的配置。如果您想要完整的上下文,请使用@SpringBootTest。最重要的是,控制器代码(和服务代码)可以帮助我们找到依赖问题 -
@Lesiak 你说得对,我误解了我想如何在集成测试中使用 MockBean。最后我做到了:
@ExtendWith(SpringExtension.class) @WebMvcTest(controllers = DeviceController.class) class DeviceControllerTest { @Autowired private MockMvc mockMvc; @MockBean private DeviceRepository deviceRepository; @MockBean private DeviceService deviceService; @MockBean private RestTemplate restTemplate; }它正在工作
标签: java mongodb junit junit5 mongotemplate