开发者

Spring MVC test case

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 f开发者_运维百科inal 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.


"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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜