0

我有一个 JUnit 测试:

@Autowired
private UserController userController;

@Test
public void getUser() throws Exception {
    User user = jUnitDAOHelper.createUser();

    Map<String, Object> params = new HashMap<String, Object>();

    params.put("email", user.getUserDetail().getEmail());

    String result = sendRequest("/user", GET, userController, params);
    assertNotNull(result);
    System.out.println("result="+result);
}

这是我的控制器(片段):

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    // deleted
}

这是我的 sendRequest 方法:

public String sendRequest(String url, String method, Object controller, Map<String, Object> params) throws Exception {
        request.setRequestURI(url);
        request.setParameters(params);
        request.setMethod(method);
        request.setContentType("application/json");
        handlerAdapter.handle(request, response, controller);
        return response.getContentAsString();
    }

抛出此错误:

org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException: No matching handler method found for servlet request: path '/user', method 'GET', parameters map['email' -> array<String>['test@test.com']]

处理适配器:

    protected MockHttpServletRequest request;
    protected MockHttpServletResponse response;
    protected HandlerAdapter handlerAdapter;

    @Before
    public void setUp() {
       request = new MockHttpServletRequest();
       response = new MockHttpServletResponse();
       handlerAdapter = applicationContext.getBean(HandlerAdapter.class);
    }

任何想法出了什么问题?我相信每件事都被正确映射,但它一直在抛出这个。

4

2 回答 2

0

您编写了一个控制器类......但没有实际的方法来处理请求!您需要编写一个接受参数并产生结果的方法。

于 2013-09-25T14:48:09.647 回答
0

请注意,您尝试调用的方法

handlerAdapter.handle(request, response, controller);

来自HandlerAdapter界面。

ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;

我几乎确信您正在使用已弃用 的,如果找不到,则AnnotationMethodHandlerAdapter在尝试resolveHandlerMethod()抛出时。NoSuchRequestHandlingMethodException

UserController BaseController有任何处理方法吗?看起来不像。

于 2013-09-25T14:51:48.213 回答