当在Spring-boot中使用@WebMvcTest时如何排除@Configuration类?

问题描述 投票:0回答:2

我正在为休息控制器写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
    }

}

控制器

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

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

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

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);
    }
}
java spring-boot junit4
2个回答
0
投票

当您的测试类位于测试文件夹中并以“ test”配置文件运行时,您的实际配置类将被排除。这是我如何配置Spring Boot测试的示例。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = IntegrationApplication.class,
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@ActiveProfiles("test")
public class MyControllerTest {

=================================>

@SpringBootApplication
public class IntegrationApplication {

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

0
投票

您对@ComponentScan的使用已禁用@WebMvcTest用来限制通过扫描找到的组件类型的过滤器。

© www.soinside.com 2019 - 2024. All rights reserved.