Junit 休息控制器抛出登录 url 而不是实际模型和视图
Junit rest controller throws login url instead of Actual model and view
我正在尝试测试我的 spring-boot rest 控制器,它给出的是登录页面而不是实际结果。它给出了 302 状态,我已经给出了控制器代码,测试用例和 junit.Please 给出的错误已经查看了所有代码并告诉我你需要任何其他信息
控制器
@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;
}
联合测试
@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
尝试禁用 spring 安全性,因为它正在重定向到登录页面。
@AutoConfigureMockMvc(addFilters = false)
secure=false 应该也可以工作,但可能会被弃用,具体取决于您的 Spring
版本
我正在尝试测试我的 spring-boot rest 控制器,它给出的是登录页面而不是实际结果。它给出了 302 状态,我已经给出了控制器代码,测试用例和 junit.Please 给出的错误已经查看了所有代码并告诉我你需要任何其他信息
控制器
@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;
}
联合测试
@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
尝试禁用 spring 安全性,因为它正在重定向到登录页面。
@AutoConfigureMockMvc(addFilters = false)
secure=false 应该也可以工作,但可能会被弃用,具体取决于您的 Spring
版本