Junit, spring po wydzieleniu części kodu do metody przestaje działać

Junit, spring po wydzieleniu części kodu do metody przestaje działać
  • Rejestracja: dni
  • Ostatnio: dni
0

Nie działa:

Kopiuj
    private MvcResult login() throws Exception {
        String json = String.format("{ \"username\": \"%s\", \"password\": \"%s\"}", this.username, this.password);
        return this.mockMvc.perform(post("/api/auth")
                .contentType(MediaType.APPLICATION_JSON)
                .content(json))
                .andExpect(status().isOk())
                .andExpect(header().exists("Authorization"))
                .andReturn();
    }

    @Test
    public void changePassword() throws Exception {
        String header = this.login().getResponse().getHeader("Authorization");
        String passwordJson = String.format("{ \"oldPassword\": \"%s\", \"newPassword\": \"%s\"}", this.password, this.newPassword);
        mockMvc.perform(put("/api/user/password/change")
                .contentType(MediaType.APPLICATION_JSON)
                .content(passwordJson)
                .header("Authorization", header))
                .andExpect(status().isOk());

Działa:

Kopiuj
    @Test
    public void changePassword() throws Exception {
        String json = String.format("{ \"username\": \"%s\", \"password\": \"%s\"}", this.username, this.password);
        MvcResult mvcResult = mockMvc.perform(post("/api/auth")
                .contentType(MediaType.APPLICATION_JSON)
                .content(json))
                .andExpect(status().isOk())
                .andExpect(header().exists("Authorization"))
                .andReturn();

        String header = mvcResult.getResponse().getHeader("Authorization");
        String passwordJson = String.format("{ \"oldPassword\": \"%s\", \"newPassword\": \"%s\"}", this.password, this.newPassword);
        mockMvc.perform(put("/api/user/password/change")
                .contentType(MediaType.APPLICATION_JSON)
                .content(passwordJson)
                .header("Authorization", header))
                .andExpect(status().isOk());

Dlaczego? Zwraca po prostu zły status code.

  • Rejestracja: dni
  • Ostatnio: dni
0

niewazne

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.