Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration tests on spring-boot throws Connection refused

I have an unit test on Spring Boot:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class CustomerControllerIT {
    private RestTemplate restTemplate = new RestTemplate();
    @Test
    public void findAllCustomers() throws Exception {
        ResponseEntity<List<Customer>> responseEntity = restTemplate.exchange(
                "http://localhost:8080/Customer", HttpMethod.GET, null,
                new ParameterizedTypeReference<List<Customer>>() {
                });
        List<Customer> list = responseEntity.getBody();
        Assert.assertEquals(list.size(), 0);
    }
}
  • If I launch test on started application - tests ok

  • If I try to launch only IT, there is connection refused error

My application.properties is same for single start.

For tests and located in resources and testResources.

Application.class is:

@ComponentScan({"mypackage"})
@EntityScan(basePackages = {"mypackage.model"})
@EnableJpaRepositories(basePackages = {"mypackage.persistence"})
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
like image 225
Roberto Avatar asked Sep 14 '18 16:09

Roberto


2 Answers

You must run a test with a running server ,

If you need to start a full running server, you can use random ports:

  • @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)

An available port is picked at random each time your test runs

You need this maven dependency:

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

Example:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class TestRest {

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void findAllCustomers() throws Exception {
        ResponseEntity<List<Customer>> responseEntity = restTemplate.exchange(
               "/Customer", HttpMethod.GET, null,
               new ParameterizedTypeReference<List<Customer>>(){});
        List<Customer> list = responseEntity.getBody();
        Assert.assertEquals(list.size(), 0);
    }
}

Please read the documentation for more reference

like image 161
Fernix Avatar answered Sep 22 '22 20:09

Fernix


For running @SpringBootTest and JUnit5 (Jupiter) with static port you can use:

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class MyUnitTests {
    @Test
    void checkActuator() throws Exception {
        final String url = "http://localhost:8080/actuator/health";
        @SuppressWarnings("rawtypes")
        ResponseEntity<Map> re = new RestTemplate().getForEntity(url, Map.class);
        System.out.println(re);
        assertEquals(HttpStatus.OK, re.getStatusCode());
        re.getStatusCode();
    }
}

If you're using JUnit 4:

  • Change: @ExtendWith(SpringExtension.class)
  • To: @RunWith(SpringRunner.class)

And then add the port property to your application.yml (inside folder src/main/resources):

server:
  port: 8080

Or if have an application.properties:

server.port=8080

Reference:

  • How to configure port for a Spring Boot application

  • https://www.baeldung.com/spring-boot-change-port

  • Test the SpringBoot application startup

like image 37
ℛɑƒæĿᴿᴹᴿ Avatar answered Sep 22 '22 20:09

ℛɑƒæĿᴿᴹᴿ