Spring Boot测试不会启动上下文或加载依赖项

问题描述 投票:0回答:1

一个非常初学者的问题,但我无法克服。我有一个基本的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
1个回答
0
投票

当使用@Autowired批注时,基本上是将变量与应用程序上下文中存在的对象进行映射。请记住,启动Spring Boot应用程序时会创建应用程序上下文。具有注释@Service@Repository@Component的所有类都在应用程序上下文中实例化。

我假设SampleRepository具有以下注释之一:@Service@Repository@Component @Repository。并且,当您启动spring boot应用程序时,将创建应用程序上下文并实例化SampleRepository类。

@Autowire批注将映射在应用程序上下文中创建的对象与带有注释@Autowire的变量。]​​>

之所以在您的测试中不起作用的原因是SampleRepository类的对象不存在。而且您无法将其映射到用@Autowire注释的变量。]​​>

您可以通过两种方式解决此问题:

  1. 第一个解决方案是在运行测试类时创建应用程序上下文。我建议不要使用所有实例化的对象加载整个应用程序上下文。最好只加载测试类中需要的应用程序上下文部分。
    @EnableConfigurationProperties(SampleRepository.class)
       public class TestConfiguration {
    }
    
    @ExtendWith(SpringExtension.class)
    @SpringBootTest(classes = { TestConfiguration.class })
    public class SampleServiceTest{
    }
    
    1. 第二种解决方案是如下修改注释@DataMongoTest
    @DataMongoTest(includeFilters = @Filter(Service.class))
    //or
    @DataMongoTest(includeFilters = @Filter(Component.class))
    

    使用@DataMongoTest批注将禁用完全自动配置,而仅应用与MongoDB测试相关的配置。因此,没有实例化用@ServicesComponent注释的类。 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]");    
    }

}

下面是结果:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.