我想在我的控制器中為洗掉方法撰寫一個集成測驗,但是無論我嘗試重寫多少次,我仍然會遇到不同的錯誤。這是我的控制器中的方法:
@Controller
public class AgencyController {
@Autowired
private AgencyService agencyService;
@Autowired
private AgencyMapper agencyMapper;
@GetMapping("/deleteAgencyPage/{id}")
public String deleteAgencyPage(@PathVariable(value = "id") Long id) {
agencyService.deleteById(id);
return "redirect:/";
}
}
這是測驗,簡單明了,只是呼叫方法并檢查狀態:
@SpringBootTest
@TestPropertySource(locations = "classpath:application-h2.properties")
@AutoConfigureMockMvc
@WithMockUser(username = "[email protected]")
@ActiveProfiles("H2")
public class AgencyControllerTest {
@Autowired
MockMvc mockMvc;
@MockBean
AgencyService agencyService;
@MockBean
AgencyMapper agencyMapper;
@MockBean
Model model;
@Test
public void deleteAgency() throws Exception {
mockMvc.perform(get("/deleteAgencyPage/{id}",1L))
.andExpect(status().isOk());
}
}
這就是我得到的:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /deleteAgencyPage/1
Parameters = {}
Headers = []
Body = <no character encoding set>
Session Attrs = {SPRING_SECURITY_CONTEXT=SecurityContextImpl [Authentication=UsernamePasswordAuthenticationToken [Principal=org.springframework.security.core.userdetails.User [Username=user1@gmail.com, Password=[PROTECTED], Enabled=true, AccountNonExpired=true, credentialsNonExpired=true, AccountNonLocked=true, Granted Authorities=[ROLE_USER]], Credentials=[PROTECTED], Authenticated=true, Details=null, Granted Authorities=[ROLE_USER]]]}
Handler:
Type = com.example.awbdproject.controllers.AgencyController
Method = com.example.awbdproject.controllers.AgencyController#deleteAgencyPage(Long)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = redirect:/
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 302
Error message = null
Headers = [Content-Language:"en", X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY", Location:"/"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = /
Cookies = []
java.lang.AssertionError: Status expected:<200> but was:<302>
Expected :200
Actual :302
我做錯了什么?這對我來說似乎是一個簡單的測驗,但我顯然錯過了一些東西。
uj5u.com熱心網友回復:
集成測驗作業正常。例如,“home”回傳值為您呈現的模板提供 HTTP 200 Ok狀態。在這里,重定向導致 HTTP 302 Found狀態。此回應告訴客戶端(瀏覽器)資源已(暫時)移動。
將測驗更改為.andExpect(status().isFound())將使其成功。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/463700.html
下一篇:在SpringAuthorizationServer(0.2.3 )中支持并發全堆疊MVC(session)認證以及無狀態JWT認證
