我有一個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>['[email protected]']]
的HandlerAdapter:
protected MockHttpServletRequest request;
protected MockHttpServletResponse response;
protected HandlerAdapter handlerAdapter;
@Before
public void setUp() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
handlerAdapter = applicationContext.getBean(HandlerAdapter.class);
}
任何想法是怎麼回事?我相信每一件事都是正確的映射,但它一直在拋出這一點。
向我們展示你的'handlerAdapter'。我認爲你使用不正確。 –
只是因爲彈簧連接的物體,並不意味着請求映射連線 –
你能告訴我們處理程序的方法嗎? –