使用RestEasy的Spring MVC的Junit测试用例

时间:2011-05-12 06:40:07

标签: spring-mvc junit resteasy

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

带resteasy的控制器。我尝试了junit的示例实现 测试案例,但获得例外。

我的代码是.....

spring MVC控制器

@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测试用例

@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();
        }

    }
}

异常

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)

我需要弹簧mvc的测试用例和resteasy .... Thanx提前!

1 个答案:

答案 0 :(得分:0)

当我检查MVC集成时,它不起作用。我切换到普通的Spring集成" 40.1。基本整合"。

此致