Springboot:如何执行具有实际依赖性的集成测试? [英] Springboot: How can I perform an integration test with real dependencies?

查看:80
本文介绍了Springboot:如何执行具有实际依赖性的集成测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在开始学习Java和Spring引导,并且在集成测试中遇到依赖注入方面的一些问题。我在 src / main / java / com / rfd / domain / services 下有一个名为TransactionService的类,该类被标记为@Service,并且具有另一个依赖项,其中一个依赖项Spring Boot创建的存储库。当我启动应用程序时,它会正确启动,因此我认为依赖关系已正确解决。这是概括的类:

I'm starting to learn both Java and Spring boot now, and I'm having some problems with dependency injection in integration tests. I have a class under src/main/java/com/rfd/domain/services called TransactionService, which is marked as @Service and which has another dependencies, one of them a repository created by Spring boot. When I launch the application, it is launched correctly so I assume the dependencies are being resolved correctly. This is the summarized class:

package com.rfd.domain.services;

import allNeededImports

@Service
public class TransactionsService {

    @Autowired
    private KambiTransactionRepository kambiTransactionRepository;

    @Autowired
    private TransactionFactory transactionFactory;

    public List<Transaction> retrieveTransactions(String couponExternalId) throws InvalidTransactionException {
        // someCode
    }
}

现在,我在 / src / test / java / com / rfd / integrationtests / domain / services 中有一个TransactionsServiceTests类:

and now, I have a TransactionsServiceTests class under /src/test/java/com/rfd/integrationtests/domain/services:

package com.rfd.integrationtests.domain.services;
import allNeededImports

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Main.class)
@DataMongoTest
@TestPropertySource(locations = "classpath:application-integrationtest.properties")
public class TransactionsServiceTests {

    @Autowired
    private TransactionsService transactionsService;

    @Test
    public void retrieveTransactions_happyPathMultipleTransactions_transactionsRetrieved() throws InvalidTransactionException {
        // test code
    }

当我尝试启动测试时,收到以下错误:

When I try to launch the tests, I receive the following error:


造成原因:
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'com.rfd.domain.services.TransactionsService'的
合格bean:
可用:预期至少有一个合格的bean作为自动汇款
的候选人。依赖注释:
{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.rfd.domain.services.TransactionsService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

I试图创建自己的@TestConfiguration类,在其中创建一个标有@Bean的方法并返回新的TransactionService实例,该方法可以正常工作。但是,现在的错误是由于KambiTransactionRepository依赖关系,而我没有实现,因为它是通过spring boot给出的:

I have tried to create my own @TestConfiguration class, in which I create a method marked with @Bean and returning a new instance of TransactionService, and it works. However, the error now is for the KambiTransactionRepository dependency, and I don't have an implementation of it because it is given by spring boot:

package com.rfd.infrastructure.repositories;

import com.rfd.infrastructure.models.KambiTransaction;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface KambiTransactionRepository extends MongoRepository<KambiTransaction, String> {

    List<KambiTransaction> findByCouponRef(String couponRef);
}

问题
如何执行

QUESTION How can I execute the integration test using the dependency resolution of the main code?

推荐答案

如@ M.Deinum在注释中所述, @SpringBootTest @DataMongoTest 是互斥的,因此删除 @DataMongoTest 解决了问题。

As @M.Deinum remarked in comments, @SpringBootTest and @DataMongoTest are mutually exclusive, so removing @DataMongoTest solved the problem.

但是,如果您仍然想使用 @DataMongoTest 批注,您可以使用以下句子:

However, if you still want to use the @DataMongoTest annotation, you can use this sentence:

@DataMongoTest(includeFilters = @ComponentScan.Filter(Service.class))

这样,所有带有 @Component 注释的类加载并自动接线。其中包括 @Service @存储库 @Controller

That way, all classes that are annotated with @Component will be loaded and autowired. This includes (among others) @Service, @Repository and @Controller.

这篇关于Springboot:如何执行具有实际依赖性的集成测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆