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

HelloControllerITTest.java 1.2KB

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