简体   繁体   English

如何用 mockito 测试这个类?

[英]How to test this class with mockito?

I implemented REST API using JWT token.我使用 JWT 令牌实现了 REST API。 User can signup new account, or login with username & password.用户可以注册新帐户,或使用用户名和密码登录。 I have a class called AuthController.我有一个名为 AuthController 的类。 I need to test two methods: login/register.我需要测试两种方法:登录/注册。 I wanna use Mockito to test this class.我想用 Mockito 来测试这个类。

How to mock authentication(token)?如何模拟身份验证(令牌)?

@RestController
@RequestMapping(value = "/api/auth")
public class AuthController {
    private final AuthenticationManager authenticationManager;
    private final JwtTokenUtils jwtToken;
    private final UserService userService;
    private final UserRepository repository;
    private final PasswordEncoder encoder;
    private final RoleRepository roleRepository;

    @Autowired
    public AuthController(AuthenticationManager authenticationManager, JwtTokenUtils jwtToken, UserService userService, UserRepository repository, PasswordEncoder encoder, RoleRepository roleRepository) {
        this.authenticationManager = authenticationManager;
        this.jwtToken = jwtToken;
        this.userService = userService;
        this.repository = repository;
        this.encoder = encoder;
        this.roleRepository = roleRepository;
    }
        

Method: /login方法:/登录

    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody AuthDto requestDto) {
        try {
            String username = requestDto.getUsername();
            Authentication authentication = authenticationManager.authenticate(
                    new UsernamePasswordAuthenticationToken(requestDto.getUsername(), requestDto.getPassword()));
            User user=userService.findByUsername(username);

            SecurityContextHolder.getContext().setAuthentication(authentication);
            String jwt = jwtToken.generateJwtToken(authentication);

            UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
            List<String> roles = userDetails.getAuthorities().stream()
                    .map(GrantedAuthority::getAuthority)
                    .collect(Collectors.toList());

            return ResponseEntity.ok(new JwtResponseDto(
                    jwt,
                    userDetails.getId(),
                    userDetails.getUsername(),
                    userDetails.getEmail(),
                    roles));

        } catch (AuthenticationException e) {
            throw new BadCredentialsException("Invalid username or password");
        }
    }

Method:/signUp方法:/注册

    @PostMapping("/signup")
    public ResponseEntity<?> registerUser(@RequestBody CustomerDto signUpAuthDto) {
        if (repository.existsByUsername(signUpAuthDto.getUsername())) {
            return ResponseEntity
                    .badRequest()
                    .body(new MessageResponse("Error: Username is already taken"));
        }
        if (repository.existsByEmail(signUpAuthDto.getEmail())) {
            return ResponseEntity
                    .badRequest()
                    .body(new MessageResponse("Error: Email is already in use"));
        }
        if (signUpAuthDto.getPassword() !=null && !signUpAuthDto.getPassword().equals(signUpAuthDto.getConfirm())) {
            return  ResponseEntity
                    .badRequest()
                    .body(new MessageResponse("Error: You entered two different passwords. Please try again"));
        }

        User user = new User(signUpAuthDto.getUsername(),
                signUpAuthDto.getEmail(),
                encoder.encode(signUpAuthDto.getPassword()));
                encoder.encode(signUpAuthDto.getConfirm());

        Set<Role> strRoles = signUpAuthDto.getRole();
        Set<Role> roles = new HashSet<>();

        if (strRoles == null) {
            Role userRole = roleRepository.findByName(EnumRole.ROLE_USER)
                    .orElseThrow(()-> new RuntimeException("Error: Role is not found"));
            roles.add(userRole);
        } else {
            strRoles.forEach(role -> {
                if ("admin".equals(role)) {
                    Role adminRole = roleRepository.findByName(EnumRole.ROLE_ADMIN)
                            .orElseThrow(()-> new RuntimeException("Error: Role is not found"));

                    roles.add(adminRole);
                } else {
                    Role userRole = roleRepository.findByName(EnumRole.ROLE_USER)
                            .orElseThrow(()-> new RuntimeException("Error: Role is not found"));
                    roles.add(userRole);
                }
            });
        }

        user.setRoles(roles);
        repository.save(user);

        return ResponseEntity.ok(new MessageResponse("User registered successfully!"));

    }
}

Any help is much appreciated!任何帮助深表感谢!

The best way to mock authentication here is using one of the SecurityMockMvcRequestPostProcessors of the Spring Security Test dependency:这里模拟身份验证的最佳方法是使用 Spring Security Test 依赖项的SecurityMockMvcRequestPostProcessors之一:

<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-test</artifactId>
  <scope>test</scope>
</dependency>

So your Spring MVC controllers you can write a test with @WebMvcTest and get an auto-configured MockMvc instance.因此,您的 Spring MVC 控制器可以使用@WebMvcTest编写测试并获得自动配置的MockMvc实例。

This way you test your endpoint with a mocked Servlet environment and you can mock any authentication or logged-in user you want.通过这种方式,您可以使用模拟的 Servlet 环境测试您的端点,并且您可以模拟您想要的任何身份验证或登录用户。

this.mockMvc
    .perform(
      post("/api/auth/signup")
        .contentType(MediaType.APPLICATION_JSON)
        .content("YOUR_PAYLOAD")
        .with(csrf())
        .with(SecurityMockMvcRequestPostProcessors.user("duke"))
    )
    .andExpect(status().isOk())

More information about this in the following MockMvc guide .以下MockMvc 指南中有关此的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM