Building an Application with Spring Boot :: Learn how to build an application with minimal configuration. https://spring.io/guides/gs/spring-boot/

HelloControllerIT.java 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package hello;
  2. import static org.hamcrest.Matchers.*;
  3. import static org.junit.Assert.*;
  4. import java.net.URL;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.boot.test.context.SpringBootTest;
  10. import org.springframework.boot.test.web.client.TestRestTemplate;
  11. import org.springframework.boot.web.server.LocalServerPort;
  12. import org.springframework.http.ResponseEntity;
  13. import org.springframework.test.context.junit4.SpringRunner;
  14. @RunWith(SpringRunner.class)
  15. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  16. public class HelloControllerIT {
  17. @LocalServerPort
  18. private int port;
  19. private URL base;
  20. @Autowired
  21. private TestRestTemplate template;
  22. @Before
  23. public void setUp() throws Exception {
  24. this.base = new URL("http://localhost:" + port + "/");
  25. }
  26. @Test
  27. public void getHello() throws Exception {
  28. ResponseEntity<String> response = template.getForEntity(base.toString(),
  29. String.class);
  30. assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));
  31. }
  32. }