我想使用@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);
}
}
答案 0 :(得分:2)
您必须将所有特定于区域的配置(例如@ComponentScan
和@EnableMongoRepositories
)移到单独的@Configuration
文件中。重要的是不要用特定于其功能特定区域的配置设置来乱扔应用程序的主类。