8

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);
    }
}

2 Answers 2

14

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

Sign up to request clarification or add additional context in comments.

1 Comment

I had to add server.address in test app.yml to force it to use specific port.
8

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:

1 Comment

Thanks! For my Junit5 test I changed <code>@RunWith to @ExtendWith </code>., so: <code>@ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) </code>,and then my tests worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.