Support dynamic port in test

This commit is contained in:
Ryan Baxter
2016-11-08 13:58:31 -05:00
parent 8d50263207
commit 5ba58e8b9a
3 changed files with 27 additions and 5 deletions

View File

@@ -1,6 +1,8 @@
package demo;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
@@ -10,6 +12,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.loadbalancer.BaseLoadBalancer;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
@@ -22,6 +25,9 @@ import com.netflix.loadbalancer.Server;
@RestController
@RibbonClient(name = "self", configuration = TomcatApp.SelfRibbonClientConfiguration.class)
public class TomcatApp {
@Value("${server.port}")
int port;
public static void main(String[] args) {
SpringApplication.run(TomcatApp.class, args);
}
@@ -31,7 +37,6 @@ public class TomcatApp {
return "GOOD" + (value == null ? "" : " " + value) + "!";
}
// Load balancer with fixed server list for "hello" pointing to example.com
@Configuration
class SelfRibbonClientConfiguration {
@@ -40,7 +45,7 @@ public class TomcatApp {
//because of this, it doesn't use eureka to lookup the server,
// but the classpath is tested
BaseLoadBalancer balancer = new BaseLoadBalancer();
balancer.setServersList(Arrays.asList(new Server("localhost", 1234)));
balancer.setServersList(Arrays.asList(new Server("localhost", port)));
return balancer;
}

View File

@@ -4,12 +4,11 @@ zuul:
path: /self/**
example:
path: /goodurl/**
url: http://localhost:1234/good
url: http://localhost:${server.port}/good
set-content-length: true
spring:
application:
name: zuul-app
server:
port: 1234
port: 1234

View File

@@ -1,12 +1,16 @@
package demo;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.SocketUtils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
@@ -18,6 +22,20 @@ import static org.hamcrest.Matchers.is;
@RunWith(SpringRunner.class)
public class TomcatAppTests {
@BeforeClass
public static void beforeClass() {
int randomPort = SocketUtils.findAvailableTcpPort();
System.setProperty("server.port", String.valueOf(randomPort));
}
@AfterClass
public static void afterClass() {
System.clearProperty("server.port");
}
@Autowired
SpringClientFactory clientFactory;
@Autowired
private TestRestTemplate restTemplate;