A
A
avgona_pl2021-06-09 12:02:01
Java
avgona_pl, 2021-06-09 12:02:01

Mocking and Testings - why does Null crash in two tests?

Hello everyone, I ran into such a problem when the application is launched, everything works fine and works fine, but at the same time when I try to do tests, an error falls, they say, I fall null in the check for creating a token and checking the url for registration.
In addition to the code below, the server.servlet.contextPath=/api flag is also set in the properties

@AutoConfigureWebMvc
@WebMvcTest(AuthController.class)
@ExtendWith(SpringExtension.class)
class AuthControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    @Qualifier("userDetailsServiceImpl")
    private UserDetailsService userDetailsService;
    @MockBean
    private JwtUtil jwtUtil;
    @MockBean
    private JwtTokenFilter jwtTokenFilter;
    @MockBean
    private PasswordService passwordService;
    @MockBean
    private AuthenticationManager authenticationManager;
    @MockBean
    private UsersService userService;
    @MockBean
    private BCryptPasswordEncoder passwordEncoder;
    @MockBean
    private JwtAuthEntryPointJwt jwtAuthEntryPointJwt;

    private ObjectMapper objectMapper = new ObjectMapper();
    private RegistrationRequest registration = new RegistrationRequest();

    @Test
    void shouldNotBeNull() {
        Assert.notNull(mockMvc);
    }

    @Test
    void shouldCreateToken(){
        UserDetailsImpl userDetails = returnUserDetails();
        String token = jwtUtil.createToken("userDetails.getEmail()", Collections.singleton(new SimpleGrantedAuthority("ADMIN")));
        Assert.notNull(token);
    }<img src="https://habrastorage.org/webt/60/c0/82/60c082e769eb4809635868.png" alt="image"/><img src="https://habrastorage.org/webt/60/c0/83/60c08309eb06c424517440.png" alt="image"/>
    @Test
    void shouldRegisterUsers() throws Exception {
        User user = User.builder().username("User Userowski").email("[email protected]")
                .roles(Collections.singletonList(new Role("1", ERole.CUSTOMER)))
                .password("123").enabled(false).activationToken("123qwe").build();
        doNothing().when(userService).create(user);

        mockMvc.perform(post("/api/register")
                .content(objectMapper.writeValueAsBytes(registration))
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk());


        verify(userService, times(1)).create(user);
    }

60c083a3aa48f685657885.png
60c083aba8395340846949.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-06-09
@xez

Fields annotated with @MockBean are injected with a mock, not the bean that is injected in the application.
The behavior of all its methods must be specially set.
Those. you call the .createToken(...) method on the mock and get null
Use the @Autowired annotation if you want to get the original bean.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question