public class HelloWorldApplicationTests { @LocalServerPort private int port;
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class EurekaServiceApplicationTests { @LocalServerPort private int port; @Autowired private TestRestTemplate testRestTemplate; @Test public void shouldStartEurekaServer() { ResponseEntity<String> entity = this.testRestTemplate.getForEntity( "http://localhost:" + this.port + "/eureka/apps", String.class); then(entity.getStatusCode()).isEqualTo(HttpStatus.OK); } }
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @DirtiesContext public class HelloWorldConfigurationTests { @LocalServerPort private int port; @Autowired private TestRestTemplate restTemplate; @Test public void testGreeting() throws Exception { ResponseEntity<String> entity = restTemplate .getForEntity("http://localhost:" + this.port + "/", String.class); assertEquals(HttpStatus.OK, entity.getStatusCode()); } }
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class ApplicationIntegrationTests { private Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); @LocalServerPort private int port = 0; @Before public void init() throws Exception { marshaller.setPackagesToScan(ClassUtils.getPackageName(GetCountryRequest.class)); marshaller.afterPropertiesSet(); } @Test public void testSendAndReceive() { WebServiceTemplate ws = new WebServiceTemplate(marshaller); GetCountryRequest request = new GetCountryRequest(); request.setName("Spain"); assertThat(ws.marshalSendAndReceive("http://localhost:" + port + "/ws", request)).isNotNull(); } }
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class HttpRequestTest { @LocalServerPort private int port; @Autowired private TestRestTemplate restTemplate; @Test public void greetingShouldReturnDefaultMessage() throws Exception { assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/", String.class)).contains("Hello World"); } }
private StorageService storageService; @LocalServerPort private int port;
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class HelloControllerIT { @LocalServerPort private int port; private URL base; @Autowired private TestRestTemplate template; @Before public void setUp() throws Exception { this.base = new URL("http://localhost:" + port + "/"); } @Test public void getHello() throws Exception { ResponseEntity<String> response = template.getForEntity(base.toString(), String.class); assertThat(response.getBody(), equalTo("Greetings from Spring Boot!")); } }
@LocalServerPort private int port;
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class GreetingTraversonIntegrationTests { @LocalServerPort private int port; @Test public void envEndpointNotHidden() throws Exception { Traverson traverson = new Traverson(new URI("http://localhost:" + this.port + "/greeting"), MediaTypes.HAL_JSON); String greeting = traverson.follow("self").toObject("$.content"); assertThat(greeting).isEqualTo("Hello, World!"); } }
protected static class TestRibbonConfig { @LocalServerPort protected int port = 0; @Bean public ServerList<Server> ribbonServerList() { return new StaticServerList<>(new Server("localhost", this.port)); } }
protected static class TestRibbonConfig { @LocalServerPort protected int port = 0; @Bean public ServerList<Server> ribbonServerList() { return new StaticServerList<>(new Server("localhost", this.port)); } }
protected static class TestBadRibbonConfig { @LocalServerPort protected int port = 0; @Bean public ServerList<Server> ribbonServerList() { return new StaticServerList<>(new Server("https", "localhost.domain.doesnot.exist", this.port), new Server("localhost", this.port)); } }
@Configuration class TestBadRibbonConfig { @LocalServerPort protected int port = 0; @Bean public ServerList<Server> ribbonServerList() { return new StaticServerList<>(new Server("https", "localhost", this.port)); } }
/** * @author Stephane Nicoll * @author Dave Syer */ @SpringBootTest(classes = Config.class, webEnvironment = WebEnvironment.RANDOM_PORT) public abstract class AbstractFullStackInitializrIntegrationTests extends AbstractInitializrIntegrationTests { @LocalServerPort protected int port; protected String host = "localhost"; @Override protected String createUrl(String context) { return "http://" + this.host + ":" + this.port + (context.startsWith("/") ? context : "/" + context); } }
protected static final Duration DURATION = Duration.ofSeconds(5); @LocalServerPort protected int port = 0;
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) public class HelloApplicationIntegrationTest { @LocalServerPort int port; private WebTestClient client; @PostConstruct public void setup() { // Use ArmeriaClientHttpConnector if you want to send an HTTP request to the running // Spring Boot application via Armeria HTTP client. client = WebTestClient.bindToServer(new ArmeriaClientHttpConnector()) .baseUrl("http://127.0.0.1:" + port) .build(); } @Test public void helloWorld() { client.get() .uri("/hello") .exchange() .expectStatus().isOk() .expectBody(String.class).isEqualTo("Hello, World"); } }
WebTestClient testClient; @LocalServerPort int port;
public class ApplicationTests { @LocalServerPort private String port;
@LocalServerPort int port;
public class CloudFoundryRouteServiceRoutePredicateFactoryIntegrationTests extends BaseWebClientTests { @LocalServerPort int port;