一个非常初学者的问题,但我无法克服。我有一个基本的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>
当使用@Autowired
批注时,基本上是将变量与应用程序上下文中存在的对象进行映射。请记住,启动Spring Boot应用程序时会创建应用程序上下文。具有注释@Service
,@Repository
,@Component
的所有类都在应用程序上下文中实例化。
我假设SampleRepository
具有以下注释之一:@Service
,@Repository
,@Component
@Repository
。并且,当您启动spring boot应用程序时,将创建应用程序上下文并实例化SampleRepository
类。
@Autowire
批注将映射在应用程序上下文中创建的对象与带有注释@Autowire
的变量。]>
之所以在您的测试中不起作用的原因是SampleRepository
类的对象不存在。而且您无法将其映射到用@Autowire
注释的变量。]>
您可以通过两种方式解决此问题:
@EnableConfigurationProperties(SampleRepository.class)
public class TestConfiguration {
}
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = { TestConfiguration.class })
public class SampleServiceTest{
}
@DataMongoTest
:@DataMongoTest(includeFilters = @Filter(Service.class)) //or @DataMongoTest(includeFilters = @Filter(Component.class))
使用
@DataMongoTest
批注将禁用完全自动配置,而仅应用与MongoDB测试相关的配置。因此,没有实例化用@Services
,Component
注释的类。includeFilters
是一组过滤器,可用于将过滤后的bean添加到应用程序上下文中。
[我怀疑您已经用SampleRepository
或@Service
注释为类@Component
注释,这就是为什么它没有创建SampleRepository
类的实例。
我在git repo中查看了您的代码,并进行了如下修改:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class BusinessOwnerServiceTest {
@Autowired
private BusinessOwnerService businessOwnerService;
@Autowired
private BusinessOwnerRepository businessOwnerRepository;
@Test
public void shouldCreateNewBusinessOwner(){
businessOwnerService.findBusinessOwnerByEmail("[email protected]");
}
}