我有一个 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);
}
任何想法出了什么问题?我相信每件事都被正确映射,但它一直在抛出这个。