Consul discoveryClient in test

Viewed 64

I am using trying to setup project with Consul. I can start my SpringBoot with Consul running. But how can I do test in Maven build? The build server does not have Consul install and running

package com.example.demo2;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryClient;

import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@SpringBootTest
class Demo2ApplicationTests {
    @Autowired
    private ConsulDiscoveryClient discoveryClient;
    @Test
    void contextLoads() {
    }
    @Test
    public void testClient() {
        List<ServiceInstance> instances = this.discoveryClient.getInstances("testConsulDiscovery");
        assertThat(instances).as("instances was null").isNotNull();
        assertThat(instances.isEmpty()).as("instances was empty").isFalse();

        ServiceInstance instance = instances.get(0);
        assertThat(instance.isSecure()).as("instance was secure (https)").isFalse();
        assertIpAddress(instance);
        assertThat(instance.getMetadata()).containsEntry("foo", "bar");
    }

}

The following error was received

com.ecwid.consul.transport.TransportException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:8500 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: no further information

0 Answers
Related