由于缺少依赖项,@WebMvcTest 未运行 [英] @WebMvcTest not running due to missing dependency

查看:23
本文介绍了由于缺少依赖项,@WebMvcTest 未运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 @WebMvcTest 测试我的控制器.我@MockBean 控制器的依赖项,但在运行测试时,它无法启动.应用程序在运行主类时正确启动.

I want to test my controller by using @WebMvcTest. I @MockBean the dependencies of the controller but when running the test, it fails to start. The application starts correctly when running the main class.

测试:

@RunWith(SpringRunner.class)
@WebMvcTest(MetricResource.class)
public class MetricResourceTest {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private MetricService metricService;

    @MockBean
    private MetricMapper metricMapper;

    @Test
    public void test() {
    }

}

控制器:

@RestController
@RequestMapping("/api/v1/metrics")
public class MetricResource {

    private final MetricService metricService;

    private final MetricMapper metricMapper;

    public MetricResource(MetricService metricService, MetricMapper metricMapper) {
        this.metricService = metricService;
        this.metricMapper = metricMapper;
    }

    @GetMapping
    public ResponseEntity<List<MetricDto>> getMetrics(@RequestParam(required = false) List<String> fields) {
        if (fields == null) {
            fields = new ArrayList<>();
        }
        List<Metric> metrics = metricService.getMetric(fields);
        List<MetricDto> dto = metricMapper.fromMetric(metrics);
        return ResponseEntity.ok(dto);
    }

}

错误:

Description:

Parameter 2 of constructor in com.sps.soccer.service.SoccerService required a bean named 'mongoTemplate' that could not be found.

Action:

Consider defining a bean named 'mongoTemplate' in your configuration.

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'soccerService' defined in file [K:\home\projects\stable\sps-backend\sps-soccer\target\classes\com\sps\soccer\service\SoccerService.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'soccerAnalysisRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' available

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'soccerAnalysisRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' available

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' available

SoccerService 依赖于 SoccerAnalysisRepository,它是一个 MongoRepository.我不明白为什么 SoccerService 是由测试创建的,因为 @Service 没有被 @WebMvcTest 扫描.

The SoccerService has a dependency on SoccerAnalysisRepository which is a MongoRepository. I don't understand why the SoccerService is created by the test since @Service are not scanned by @WebMvcTest.

应用程序是一个Maven多模块,所以我必须显式配置组件扫描和存储库.

The application is a Maven multi modules, so I has to explicitly configure the component scanning and repository.

@SpringBootApplication
@ComponentScan(basePackages = {"com.sps.soccer", "com.sps.sdql", "com.sps.core", "com.sps.web"},
        excludeFilters = {
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = com.sps.sdql.configuration.ClockConfiguration.class),
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = com.sps.soccer.configuration.ClockConfiguration.class),
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = com.sps.sdql.configuration.RestConfiguration.class),
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = com.sps.soccer.configuration.RestConfiguration.class)
        })
@EnableMongoRepositories(basePackages = {"com.sps.soccer", "com.sps.sdql", "com.sps.core"})
public class SpsWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpsWebApplication.class, args);
    }
}

推荐答案

您必须将所有特定于区域的配置,例如 @ComponentScan@EnableMongoRepositories,移动到单独的@Configuration 文件.不要在应用程序的主类中放置特定于其功能特定区域的配置设置,这一点很重要.

You must move all area-specific configuration, like @ComponentScan and @EnableMongoRepositories, to a separate @Configuration file. It's important not to litter the application’s main class with configuration settings that are specific to a particular area of its functionality.

更多信息:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#boot-features-testing-spring-boot-应用程序测试用户配置

这篇关于由于缺少依赖项,@WebMvcTest 未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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