Junit test case for spring MVC with RestEasy

I'm newbie to spring mvc & had a problem with Junit test case for spring MVC

controller with resteasy.I tried out for sample implementation of junit test case, but getting exception.

my code is.....

spring MVC controller

@Controller
@Path("/sample")
public class SampleController {

    @Autowired
    Article article;

    public SampleController() {
        System.out.println("cons()");
    }
    @GET
    @Path("/get")
    @Produces("text/html")
    public String testPost()
    {
       System.out.println("Inside Get");
       article.getName();
       return "Hello";
    }
}

Junit Test Case for controller

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/springmvc-servlet.xml" )
public class SampleControllerTest {

    @Autowired
    protected ApplicationContext context;
    protected MockHttpServletRequest request;
    protected MockHttpServletResponse response;

    private AnnotationMethodHandlerAdapter adapter;
    @Autowired
    private SampleController controller;

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

    @Test
    public void testTestPost() {
        request.setMethod("GET");
        request.setRequestURI("/sample/get");
        try {
            adapter.handle(request, response, controller);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Exception

org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException: No matching handler method found for servlet request: path '/sample/get', method 'GET', parameters map[[empty]]
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.resolveHandlerMethod(AnnotationMethodHandlerAdapter.java:626)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    at in.nic.cmf.springmvc.SampleControllerTest.testTestPost(SampleControllerTest.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

I need test case for spring mvc with resteasy.... Thanx in Advance!


When I checked it MVC integration it doesn't work. I switched to plain Spring integration "40.1. Basic Integration".

Regards

链接地址: http://www.djcxy.com/p/68076.html

上一篇: 具有多个选项卡会话解决方案的Spring MVC PRG模式

下一篇: 用于RestEasy的Spring MVC的Junit测试用例