将@WebMvcTest 与spring-boot 一起使用时如何排除@Configuration 类?

Smi*_*ile 3 java junit4 spring-boot

我正在为休息控制器编写junit。我只想加载与控制器相关的最小上下文,我认为这就是@WebMvcTest加载上下文的方式。但是 junit 正在加载完整的 Spring 上下文,并且由于无法创建某些 bean 而失败。

我已经搜索并阅读了许多关于 stackoverflow 的问题,但没有一个解决方案可以排除特定的配置。为控制器编写junit时如何加载最小上下文?或者有什么办法可以排除一些配置类?我使用的是 Spring-boot 2.2.2.RELEASE、Java 8 和 Junit 4。

Junit(我试图排除加载一些 bean 和配置,但它不起作用):

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = OrderController.class, excludeFilters = {@Filter(classes = Configuration.class), @Filter(type = FilterType.REGEX, pattern = "com\\.foo\\..*")})
@TestPropertySource(properties = { "spring.cloud.config.server.bootstrap:false" })
public class OrderControllerTest {

    @MockBean
    private OrderService orderService;


    @Autowired
    private MockMvc mockMvc;

    @Test
    public void test() throws Exception {
        mockMvc.perform(get("/internal/order/123"));
        // Further code to verify the response
    }

}
Run Code Online (Sandbox Code Playgroud)

控制器

@Slf4j
@Validated
@RestController
@RequestMapping("/internal")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping(value = "/order/{orderId}", produces = { MediaType.APPLICATION_JSON_VALUE })
    public ResponseEntity<RegularContributionOrder> retrieveRegularContributionOrder(@NotNull @PathVariable("orderId") String orderId)
            throws OrderNotFoundException {

        RegularContributionOrder order = orderService.retrieve(orderId);

        return new ResponseEntity<RegularContributionOrder>(order, HttpStatus.OK);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想从上下文加载中排除的配置类

@Slf4j
@Configuration
@EnableAspectJAutoProxy
@ImportResource({ "classpath:spring/service-config-one.xml", "classpath:spring/service-config-two.xml" })
public class OrderServiceConfig {

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:resourcebundles/error-messages.properties");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        return mapper;
    }
}
Run Code Online (Sandbox Code Playgroud)

Spring Boot 主类:

@EnableJms
@EnableAspectJAutoProxy
@ComponentScan(basePackages = { "com.foo.services", "com.bar" })
@SpringBootApplication(exclude = { SomeConfiguration.class})
public class BootApplication extends SpringBootServletInitializer {

    @Override
    protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
        return application.sources(BootApplication .class);
    }

    public static void main(final String[] args) {
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
        SpringApplication.run(BootApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

And*_*son 9

您的使用@ComponentScan已禁用用于@WebMvcTest限制扫描发现的组件类型的过滤器。

您应该@ComponentScan从主应用程序类中删除并使用basePackageson 属性@SpringBootApplication

您还应该将@EnableJms和移动@EnableAspectJAutoProxy到一个单独的@Configuration类,以便在使用@WebMvcTest. 或者,您可以完全删除它们,因为它们被 Spring Boot 的自动配置所覆盖。