230414 How to add access token value in the Junit test.

William Parker·2023년 4월 14일
0

@PostConstruct removed (some jre's don't have api of that specification)

   //InitializingBean
    @Override
    public void afterPropertiesSet() throws Exception {
        successLoginUser();
    }

    //로그인 해서 토큰 반환하는 메소드
    String successLoginUser() throws Exception {
        MvcResult result = mvc.perform(MockMvcRequestBuilders
                .post("/api/general/login")
                .accept(APPLICATION_JSON)
                .contentType(APPLICATION_JSON)
                .content(TestUtil.stringify(Map.of("email", "test@test.com", "password", "test"))))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andReturn();
        String response = result.getResponse().getContentAsString();
        return JsonPath.parse(response).read("$.accessToken");
    }
  @Test
    public void registerWithoutAuthenticate() throws Exception {
         mvc.perform(MockMvcRequestBuilders
                         .post("/api/user/register")
                         .accept(APPLICATION_JSON)
                         .contentType(APPLICATION_JSON)
                         .content(TestUtil.stringify(Map.of("email", "test", "password", "test"))))
                 .andExpect(MockMvcResultMatchers.status().isUnauthorized());
    }
    //회원가입 - 인증 성공 -> 400에러
    @Test
    public void registerWithAuthenticateMakeContentError() throws Exception {
        String accessToken = successLoginUser();
        mvc.perform(MockMvcRequestBuilders
                .post("/api/user/register").header(JwtUtil.AUTHORIZATION_HEADER,accessToken)
                .accept(APPLICATION_JSON)
                .contentType(APPLICATION_JSON)
                .content(TestUtil.stringify(Map.of("email", "test", "password", "test"))))
            .andExpect(MockMvcResultMatchers.status().isBadRequest());
    }
profile
Developer who does not give up and keeps on going.

0개의 댓글