> For the complete documentation index, see [llms.txt](https://olavo-moreira.gitbook.io/studies/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://olavo-moreira.gitbook.io/studies/testes-api-mockmvc-e-restassured/exercicios-testes-de-api-com-mockmvc/problema-2-inserir-produto/401-quando-nao-logado-como-admin-ou-cliente-token-invalido.md).

# 401 quando não logado como admin ou cliente (token inválido)

```java
    @Test
    public void insertShouldReturnUnauthorizedWhenInvalidToken() throws Exception {
        String jsonBody = objectMapper.writeValueAsString(productDTO);

        ResultActions result =
                mockMvc.perform(post("/products")
                //usando token invalido
                        .header("Authorization", "Bearer " + invalidToken)
                        .content(jsonBody)
                        .contentType(MediaType.APPLICATION_JSON)
                        .accept(MediaType.APPLICATION_JSON));
        result.andExpect(status().isUnauthorized());
    }
```
