2

Am new to Spring MVC, i have written web servise using spring MVC and resteasy. My controller is working fine, now need to write testcase but i tried writtig but i never succed am also getting problem in autowiring.

@Controller

@Path("/searchapi")
public class SearchAPIController implements ISearchAPIController {
 @Autowired
    private ISearchAPIService srchapiservice;
@GET
    @Path("/{domain}/{group}/search")
    @Produces({"application/xml", "application/json"})
    public Collections  getSolrData(
            @PathParam("domain") final String domain,
            @PathParam("group") final String group,
            @Context final UriInfo uriinfo) throws Exception {    
       System.out.println("LANDED IN get****************");
        return srchapiservice.getData(domain, group, uriinfo);
    }
}

can anyone give me sample code for Test case in spring mvc.

Taoufik Mohdit
  • 1,910
  • 3
  • 26
  • 39
thamaiyanthi
  • 21
  • 1
  • 3

2 Answers2

4

"Spring-MVC" Test case could seem like this using mock objects, for example we want to test my MyControllerToBeTest:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring.xml")
public class MyControllerTest {

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;
    private MyControllerToBeTested controller;
    private AnnotationMethodHandlerAdapter adapter;

    @Autowired
    private ApplicationContext applicationContext;

    @Before
    public void setUp() {
        request    = new MockHttpServletRequest();
        response   = new MockHttpServletResponse();
        response.setOutputStreamAccessAllowed(true);
        controller = new MyControllerToBeTested();
        adapter = new AnnotationMethodHandlerAdapter();
    }

    @Test
    public void findRelatedVideosTest() throws Exception {
        request.setRequestURI("/mypath");
        request.setMethod("GET");
        request.addParameter("myParam", "myValue");
        adapter.handle(request, response, controller);
        System.out.println(response.getContentAsString());
    }

}

but i don't have any experience with REST resource testing, in your case RestEasy.

Erhan Bagdemir
  • 5,231
  • 6
  • 34
  • 40
  • a similar solution is described in http://www.scarba05.co.uk/blog/2010/03/integration-testing-of-springs-mvc-annotation-mapppings-for-controllers/ – ilcavero Aug 19 '11 at 18:12
0

If you want to test the full service inside the container you can have a look at the REST Assured framework for Java. It makes it very easy to test and validate HTTP/REST-based services.

Johan
  • 37,479
  • 32
  • 149
  • 237