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.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package hello;
  2. import static org.hamcrest.Matchers.equalTo;
  3. import static org.junit.Assert.assertThat;
  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.Value;
  9. import org.springframework.boot.test.IntegrationTest;
  10. import org.springframework.boot.test.SpringApplicationConfiguration;
  11. import org.springframework.boot.test.TestRestTemplate;
  12. import org.springframework.http.ResponseEntity;
  13. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  14. import org.springframework.test.context.web.WebAppConfiguration;
  15. import org.springframework.web.client.RestTemplate;
  16. @RunWith(SpringJUnit4ClassRunner.class)
  17. @SpringApplicationConfiguration(classes = Application.class)
  18. @WebAppConfiguration
  19. @IntegrationTest({"server.port=0"})
  20. public class HelloControllerIT {
  21. @Value("${local.server.port}")
  22. private int port;
  23. private URL base;
  24. private RestTemplate template;
  25. @Before
  26. public void setUp() throws Exception {
  27. this.base = new URL("http://localhost:" + port + "/");
  28. template = new TestRestTemplate();
  29. }
  30. @Test
  31. public void getHello() throws Exception {
  32. ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);
  33. assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));
  34. }
  35. }