【发布时间】:2020-07-19 07:07:25
【问题描述】:
一个非常初学者的问题,但我无法通过。我有一个基本的 Spring Boot 应用程序和一个连接到云图集实例的 Spring Data MongoDB 存储库。问题是在我的 Spring Boot 测试中,我的存储库没有自动装配,并且没有创建嵌入式 MongoDB 实例。如果我启动 Spring Boot 应用程序并在主类中自动装配存储库,那么它可以工作。为什么它在我的测试中不起作用?
这是我的测试课:
@DataMongoTest
@ExtendWith(SpringExtension.class)
public class SampleServiceTest{
@Autowired
private SampleRepository sampleRepository;
@Test
public void shouldCreateSample(){
sampleRepository.save(new Sample());
}
}
这是我的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath></relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.comand</groupId>
<artifactId>business-owner-service</artifactId>
<version>1.0-SNAPSHOT</version>
<description>API Gateway</description>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
标签: spring spring-boot junit spring-data-mongodb spring-boot-test