小编典典

@WebMvcTest由于缺少依赖项而无法运行

spring-boot

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

考试:

@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

该应用程序是一个Maven多模块,因此我必须显式配置组件扫描和存储库。

@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);
    }
}

阅读 1403

收藏
2020-05-30

共1个答案

小编典典

您必须将所有特定于区域的配置(例如@ComponentScan@EnableMongoRepositories)移动到单独的@Configuration文件中。重要的是不要用特定于应用程序功能特定区域的配置设置来乱扔应用程序的主类。

更多信息:https : //docs.spring.io/spring-
boot/docs/2.0.3.RELEASE/reference/htmlsingle/#boot-features-testing-spring-
boot-applications-testing-user-configuration

2020-05-30