Conteúdo do item:

Corrigindo um BUG - Teste com conexão com Banco de dados - parte 2

Vamos começar a corrigir bugs que estão relacionados ao banco de dados

Vamos ver como criar um teste para o nosso ENDPOINT que faz uma transação no banco de dados

Abaixo estão as alterações de acordo com o vídeo

Alteração no pom.xml, devem ter as dependencias :

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

<!-- Méotodo de suporte para o Junit -->
<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>2.12.0</version>
</dependency>

Conteúdo da classe de teste explicado no vídeo:
package br.com.byiorio.api.product.chapter;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;

import javax.servlet.http.Cookie;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.util.ResourceUtils;

import br.com.byiorio.api.ByiorioApiApplication;
import br.com.byiorio.api.infra.security.JWTManager;
import br.com.byiorio.api.user.UserEntity;

@SpringBootTest(classes = ByiorioApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class AlteracaoCapituloTest {

    @Autowired
    private MockMvc mvc;

    @Autowired
    JWTManager jwtManager;

    UserEntity admin;

    @BeforeEach
    public void setup() {
        // Criando usuario com permissao de admin
        admin = new UserEntity();
        admin.setId(BigInteger.valueOf(2));
        admin.setPerfil(BigInteger.valueOf(2));
    }

    @Test
    public void chamadaSucessoAlteracaoCap() throws Exception {
        String request = FileUtils.readFileToString(
                ResourceUtils.getFile("classpath:./capitulo/alteracao/request1.json"), StandardCharsets.UTF_8.name());

        String response = FileUtils.readFileToString(
                ResourceUtils.getFile("classpath:./capitulo/alteracao/response1.json"), StandardCharsets.UTF_8.name());

        mvc.perform(
                MockMvcRequestBuilders.put("/adm/v1/product/3/chapter/3")
                        .cookie(new Cookie(HttpHeaders.AUTHORIZATION,
                                JWTManager.TOKEN_PREFIX.concat(jwtManager.createUserJWT(admin))))
                        .content(request)
                        .contentType(MediaType.APPLICATION_JSON))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().is2xxSuccessful())
                .andExpect(MockMvcResultMatchers.content().json(response));
    }

    @Test
    public void chamadaErroAlteracaoCap() throws Exception {
        String request = FileUtils.readFileToString(
                ResourceUtils.getFile("classpath:./capitulo/alteracao/request1.json"), StandardCharsets.UTF_8.name());

        String response = FileUtils.readFileToString(
                ResourceUtils.getFile("classpath:./capitulo/alteracao/response1Erro.json"),
                StandardCharsets.UTF_8.name());

        mvc.perform(
                MockMvcRequestBuilders.put("/adm/v1/product/3/chapter/39999")
                        .cookie(new Cookie(HttpHeaders.AUTHORIZATION,
                                JWTManager.TOKEN_PREFIX.concat(jwtManager.createUserJWT(admin))))
                        .content(request)
                        .contentType(MediaType.APPLICATION_JSON))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().is4xxClientError())
                .andExpect(MockMvcResultMatchers.content().json(response));
    }
}

junit5; java; teste; h2; sonarqube; bug; correção



Redirecionar para https://www.byiorio.com.br/product/11/item/43