我正在嘗試測驗我的 spring-boot rest 控制器,它提供登錄頁面而不是實際結果。它給出了 302 狀態,我已經給出了控制器代碼、測驗用例和 junit 給出的錯誤。請查看所有代碼,讓我您需要任何其他資訊
控制器
@GetMapping("{userId}/edit/{todoId}")
public ModelAndView updateTodo(@PathVariable Long userId, @PathVariable Long todoId) {
User user = userService.findById(userId);
ModelAndView model = new ModelAndView("todo-form.jsp");
model.addObject("userId",userId);
return model;
}
Junit 測驗
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
class UserApiControllerTest {
@DisplayName("Test updateTodo Api")
@Test
public void updateTodo() throws Exception {
mockMvc.perform(get("/1/edit/2"))
.andExpect(status().isOk());
}
錯誤
MockHttpServletRequest:
HTTP Method = GET
Request URI = /1/edit/2
Parameters = {}
Headers = []
Body = null
Session Attrs = {SPRING_SECURITY_SAVED_REQUEST=DefaultSavedRequest[http://localhost/1/edit/2]}
Handler:
Type = null
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 302
Error message = null
Expires:"0", Location:"http://localhost/login"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = http://localhost/login
uj5u.com熱心網友回復:
嘗試禁用 spring 安全,因為它重定向到登錄頁面。
@AutoConfigureMockMvc(addFilters = false)
secure=false 也應該有效,但可能會被棄用,具體取決于您的 Spring 版本
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/400961.html
上一篇:如何從restAPI獲取資料?
