001package com.lucidtech.puttingalltogether.samples; 002 003import org.junit.jupiter.api.Test; 004import org.springframework.beans.factory.annotation.Autowired; 005import org.springframework.beans.factory.annotation.Value; 006import org.springframework.boot.test.context.SpringBootTest; 007import org.springframework.boot.test.web.client.TestRestTemplate; 008 009import static org.assertj.core.api.Assertions.assertThat; 010import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; 011 012@SpringBootTest(webEnvironment = RANDOM_PORT) 013//It is nice to have a sanity check, but you should also write some tests that assert the behavior of your application. 014// To do that, you could start the application and listen for a connection (as it would do in production) 015// and then send an HTTP request and assert the response. 016class StartFullSpringContextWithServerRestTemplateHttpRequestIT { 017 @Value(value = "${local.server.port}") 018 private int port; 019 020 @Autowired 021 private TestRestTemplate restTemplate; 022 023 @Test 024 public void greetingShouldReturnDefaultMessage() { 025 assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/greeting", 026 String.class)).contains("World"); 027 } 028}