Selvakumar Ponnusamy Selvakumar Ponnusamy - 2 months ago 17
Java Question

Junit CXF REST API and rollback test data

I would like to write Junit test cases for REST API. The APIs are written using CXF framework.

web.xml:

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>


spring-config.xml:

<jaxrs:server id="/SomeAction" address="/SomeAction" staticSubresourceResolution="true" publishedEndpointUrl="${endpointUrl}/SomeAction">
<jaxrs:serviceBeans>
<ref bean="SomeWebService"></ref>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider"></ref>
<ref bean="exceptionMapper"></ref>
</jaxrs:providers>

</jaxrs:server>


Webservice:

@Override
@POST
@Consumes("application/json")
@Produces("application/json")
@Path("/doSome")
public WSResponse doSome(WSRequest request) {
//Do Some operation
}


Junit

@Test
public void testDoSome()
throws Exception {
mockMvc.perform(
post("/SomeAction/doSome").contentType(contentType))
.andExpect(status().isFound());
}


I can write Junit test cases for simple REST API and rollback the data after test is done using spring test. But I couldn't write when CXF framework is involved. I have two issues here.


  • The above Junit code returns 404 response instead of 200.

  • I want to
    rollback the data when test is done



Pleas help me to resolve the issues

Answer

As the name implies, the Spring MVC Test Framework is for testing Spring MVC Web and Spring MVC REST applications.

You are writing a CXF REST application which is based on JAX-RS.

Spring MVC REST is not an implementation of JAX-RS.

It is therefore not possible to test a JAX-RS application with Spring MVC Test (i.e., MockMvc).

To test your JAX-RS endpoints, you will need to use a testing framework with dedicated support for JAX-RS. For example, you may want to use the JAX-RS Testing support from CXF, or you can Google "jax-rs testing" to find other solutions.

Comments