序言:我正在學習 Java、Spring Boot 和整體......帶有 Java/Spring Boot 的 TDD。
使用的版本:
- 春季啟動 2.6.3
- 爪哇 17
- Junit5
這是我的控制器:
@RestController
@RequestMapping("/api/v1/login")
public class LoginController {
@Autowired
private JwtAuthentication jwtAuthentication;
@Autowired
private JwtTokenUtil jwtTokenUtil;
@Autowired
private JwtUserDetailService jwtUserDetailService;
@PostMapping
public ResponseEntity<?> createAuthenticationToken(@RequestBody UserEntity userEntity) throws Exception {
jwtAuthentication.authenticate(userEntity.getUsername(), userEntity.getPassword());
final UserDetails userDetails = jwtUserDetailService.loadUserByUsername(userEntity.getUsername());
final String token = jwtTokenUtil.generateToken(userDetails);
return ResponseEntity.ok(new JwtResponse(token));
}
}
相關autowired的是JwtAuthentication:
@Component
public class JwtAuthentication {
@Autowired
private AuthenticationManager authenticationManager;
private static final long serialVersionUID = -20220210203900L;
public void authenticate(String username, String password) throws BadCredentialsException {
try {
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
} catch (BadCredentialsException e) {
throw new BadCredentialsException("INVALID_CREDENTIALS", e);
}
}
}
我為自己撰寫了測驗JwtAuthentication沒有問題,現在我需要測驗控制器。
這是我的測驗:
@ExtendWith(SpringExtension.class)
@WebMvcTest(LoginControllerTest.class)
class LoginControllerTest {
@Autowired
private MockMvc mvc;
@Autowired
private ObjectMapper objectMapper;
@MockBean
private JwtAuthentication jwtAuthentication;
@Test
void testCanLogin() throws Exception {
UserEntity userEntity = new UserEntity();
userEntity.setUsername("username");
userEntity.setPassword("password");
mvc.perform(post("/api/v1/login/").contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsBytes(userEntity))).andExpect(status().isOk());
}
}
但我得到 404 而不是 200。
我讀到原因是缺少模擬底層方法。實際上,控制器上的這些測驗不會啟動整個配置(等等)。在 SO 上找不到答案 atm。
所以,我認為解決方案需要when在測驗中添加一個“簡單”:
@Test
void testCanLogin() throws Exception {
UserEntity userEntity = new UserEntity();
userEntity.setUsername("username");
userEntity.setPassword("password");
// I think I need some code here:
// pseudocode when(jwtAuthentication.authenticate("username", "password").then .... I DON'T KNOW HERE!
mvc.perform(post("/api/v1/login/").contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsBytes(userEntity))).andExpect(status().isOk());
}
uj5u.com熱心網友回復:
404 是因為沒有控制器來處理請求。
這是因為您指定了錯誤@WebMvcTest的控制器,以至于您要測驗的控制器未包含在 spring 容器中。因此更改為以下內容應該可以解決問題:
@WebMvcTest(LoginController.class)
class LoginControllerTest {
@MockBean
private JwtAuthentication jwtAuthentication;
@MockBean
private JwtTokenUtil jwtTokenUtil;
@MockBean
private JwtUserDetailService jwtUserDetailService;
}
還要注意以下幾點:
- 我洗掉
@ExtendWith(SpringExtension.class)已@WebMvcTest包含它 @WebMvcTest只會啟用與不屬于它的 web 層相關的 bean(請參閱this) 。所以你必須使用來模擬它們。JwtTokenUtilJwtUserDetailService@MockBean
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/425486.html
下一篇:在控制器中映射之前轉換特定欄位
