无法使用Apache CXF和SpringBoot运行我的测试

时间:2019-07-03 15:48:10

标签: spring-boot junit cxf java-11

我遇到应用程序问题。我使用SpringBoot和Apache CXF制作REST端点。效果很好!

但是,当我想用​​JUnit进行单元测试时遇到问题。ApacheCXF和Springboot之间存在不兼容问题。

这仅涉及REST控制器单元测试(@RestController),我可以毫无问题地测试服务。

我尝试了几种方法来纠正我的问题 -我逐步遵循了此应用程序https://github.com/jonashackt/soap-spring-boot-cxf -我在StackOverflow上观看了很多类似的问题

CXFConfig

@Configuration
public class CXFConfig {

  @Autowired
  private Bus bus;

  @Bean
  public Server rsServer() {
    final JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
    endpoint.setProvider(new JacksonJsonProvider());
    endpoint.setBus(bus);
    endpoint.setAddress("/");
    endpoint.setServiceBeans(Arrays.<Object>asList(ProductsController()));
    return endpoint.create();
  }

  @Bean
  public ProductsController productController() {
    return new ProductsController();
  }

  @Bean
  public ServletRegistrationBean cxfServlet() {
    final ServletRegistrationBean servletRegistrationBean =
    new ServletRegistrationBean(new CXFServlet(), "/*");
    servletRegistrationBean.setLoadOnStartup(1);
    return servletRegistrationBean;
  }
}

我想测试的Controller Rest:

@RestController
public class ProductsController implements Products {

  @Autowired
  ProductService productService;

  @Override
  public PostProductsResponse postProducts(Product entity) {

    //BUISNESS PROCESS NOT SHOW HERE BUT WORKING

    return PostProductsResponse.respond201WithApplicationJson(productImpl);
  }

JUnit测试ProductsControllerTest

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = ProductsController.class)
@Import(CXFConfig.class)
public class ProductsControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    ProductServiceImpl productService;

    private ProductServiceImpl productServiceInit(){
        ProductServiceImpl product = new Product();
        product.setName("Product A");

        return product;
    }

    @Test
    public void shouldReturnProductInstantiatedWith201() {
        //Given
        Product product = this.productServiceInit();

        //When
        MvcResult result = mockMvc
                .perform(MockMvcRequestBuilders
                        .post("/products")
                        .contentType(MediaType.APPLICATION_JSON_UTF8)
                        .accept(MediaType.APPLICATION_JSON_UTF8)
                        .content(product.toString())
                )
                .andReturn();

        //Then
        Assert.assertEquals(result.getHeaders(), Products.PostProductsResponse.headersFor201());

    }

}

我得到的错误是波纹管。我想要的结果是成功的测试。

APPLICATION FAILED TO START
***************************

Description:

Field bus in pacis1.gmop.components.auxiliary.rqp.manager_service.rest.CXFConfig required a bean of type 'org.apache.cxf.Bus' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'org.apache.cxf.Bus' in your configuration.

1 个答案:

答案 0 :(得分:0)

尝试在单元测试中使用Spring-Boot的自动配置:

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = ProductsController.class)
@ContextConfiguration(classes = { CxfAutoConfiguration.class, CXFConfig.class })
public class ProductsControllerTest {
...
}