Teste unitário

JAVA EE

JUnit

08/05/2020

Bom dia, galera!

Tudo bem?

Estou fazendo teste unitário com junit e mockito no apache camel, usando jdk 11 com junit 4.12 e quando executo aparece que não é encontrado a anotação @Test.

Classe:

package br.com.santander.wac.camel.route;

import java.util.ArrayList;
import java.util.List;

import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.spring.CamelSpringBootRunner;
import org.apache.camel.test.spring.MockEndpointsAndSkip;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;

import br.com.santander.wac.camel.ClientApplication;
import br.com.santander.wac.camel.model.dto.ClientDTO;

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest(classes = {ClientApplication.class}, webEnvironment = WebEnvironment.DEFINED_PORT)
@ContextConfiguration
@MockEndpointsAndSkip("direct:*")
public class RestClientRouteBuilderTests {

    @Autowired
    CamelContext camelContext;

    @EndpointInject(uri = "mock:direct:getAll")
    private MockEndpoint mockGetAllEndpoint;

    @EndpointInject(uri = "mock:direct:get")
    private MockEndpoint mockGetEndpoint;

    @EndpointInject(uri = "mock:direct:create")
    private MockEndpoint mockCreateEndpoint;

    @EndpointInject(uri = "mock:direct:update")
    private MockEndpoint mockUpdateEndpoint;

    @EndpointInject(uri = "mock:direct:delete")
    private MockEndpoint mockDeleteEndpoint;

    private List<ClientDTO> clients;

    private ClientDTO client;

    @Before
    public void init() throws Exception {
        client = new ClientDTO(10, "Client Mock 10");

        clients = new ArrayList<ClientDTO>();
        clients.add(new ClientDTO(10, "Client Mock 10"));
        clients.add(new ClientDTO(20, "Client Mock 20"));
    }

    @DirtiesContext
    @Test
    public void mustReturnValidClients1() throws Exception {

        mockGetAllEndpoint.whenAnyExchangeReceived(exchange -> {
            exchange.getIn().setBody(clients);
            exchange.getIn().setHeader(Exchange.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
            exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, HttpStatus.OK.value());
        });

        given().log().all()
        .when()
            .get("/api/clients")
        .then()
            .log().all()
            .statusCode(HttpStatus.OK.value())
            .header(Exchange.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .body("id", hasItems(10, 20));
        
        MockEndpoint.expectsMessageCount(1, mockGetAllEndpoint);
    }

    @DirtiesContext
    @Test
    public void mustReturnValidClient2() throws Exception {

        mockGetEndpoint.whenAnyExchangeReceived(exchange -> {
            exchange.getIn().setBody(client);
            exchange.getIn().setHeader(Exchange.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
            exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, HttpStatus.OK.value());
        });

        given()
            .log().all()
        .when()
            .get("/api/clients/", client.getId())
        .then()
            .log().all()
            .statusCode(HttpStatus.OK.value())
            .header(Exchange.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .body("id", comparesEqualTo(client.getId().intValue()), "name", equalTo(client.getName()));

        MockEndpoint.expectsMessageCount(1, mockGetEndpoint);
    }

    @DirtiesContext
    @Test
    public void mustCreateClient3() throws Exception {

        mockCreateEndpoint.whenAnyExchangeReceived(exchange -> {
            exchange.getIn().setBody(client);
            exchange.getIn().setHeader(Exchange.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
            exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, HttpStatus.CREATED.value());
        });

        mockCreateEndpoint.expectedBodiesReceived(client);

        given()
            .log().all()
            .contentType(MediaType.APPLICATION_JSON_VALUE)
            .body(client)
        .when()
            .post("/api/clients")
        .then()
            .log().all()
            .statusCode(HttpStatus.CREATED.value())
            .header(Exchange.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .body("id", comparesEqualTo(client.getId().intValue()), "name", equalTo(client.getName()));
    

        mockCreateEndpoint.assertIsSatisfied();

        MockEndpoint.expectsMessageCount(1, mockCreateEndpoint);
    }


    @DirtiesContext
    @Test
    public void mustUpdateClient4() throws Exception {

        mockUpdateEndpoint.whenAnyExchangeReceived(exchange -> {
        	System.out.println(exchange.getIn().toString());
            exchange.getIn().setBody(client);
            exchange.getIn().setHeader(Exchange.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
            exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, HttpStatus.OK.value());
        });
        
        given()
            .log().all()
            .contentType(MediaType.APPLICATION_JSON_VALUE)
            .body(client)
        .when()
            .put("/api/clients/", client.getId())
        .then()
            .log().all()
            .statusCode(HttpStatus.OK.value())
            .header(Exchange.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .body("id", comparesEqualTo(client.getId().intValue()), "name", equalTo(client.getName()));

        MockEndpoint.expectsMessageCount(1, mockUpdateEndpoint);
    }

    @DirtiesContext
    @Test
   public void mustDeleteClient5() throws Exception {

        mockDeleteEndpoint.whenAnyExchangeReceived(exchange -> {
            exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, HttpStatus.NO_CONTENT.value());
        });

        given()
            .log().all()
        .when()
            .delete("/api/clients/", client.getId())
        .then().log().all()
            .statusCode(HttpStatus.NO_CONTENT.value());
        
        MockEndpoint.expectsMessageCount(1, mockDeleteEndpoint);
    }
}




Att,

Fernando Hiar.
Fernando Carvalho

Fernando Carvalho

Curtidas 0
POSTAR