Skip to content

Commit 3d3ef87

Browse files
dadadomjvmlet
authored andcommitted
Use random port for starting tomcat in tests. (#72)
1 parent 712dae9 commit 3d3ef87

File tree

1 file changed

+9
-16
lines changed
  • grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc

1 file changed

+9
-16
lines changed

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/DemoAppTest.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.lognet.springboot.grpc;
22

3-
import io.grpc.ManagedChannel;
4-
import io.grpc.ManagedChannelBuilder;
53
import io.grpc.ServerInterceptor;
64
import io.grpc.examples.CalculatorGrpc;
75
import io.grpc.examples.CalculatorOuterClass;
@@ -10,17 +8,18 @@
108
import io.grpc.health.v1.HealthCheckRequest;
119
import io.grpc.health.v1.HealthCheckResponse;
1210
import io.grpc.health.v1.HealthGrpc;
13-
import org.hamcrest.CoreMatchers;
14-
import org.junit.*;
11+
import org.junit.Assert;
12+
import org.junit.Rule;
13+
import org.junit.Test;
1514
import org.junit.runner.RunWith;
1615
import org.lognet.springboot.grpc.demo.DemoApp;
1716
import org.mockito.Mockito;
1817
import org.springframework.beans.factory.annotation.Autowired;
1918
import org.springframework.beans.factory.annotation.Qualifier;
19+
import org.springframework.boot.context.embedded.LocalServerPort;
2020
import org.springframework.boot.test.context.SpringBootTest;
2121
import org.springframework.boot.test.rule.OutputCapture;
2222
import org.springframework.boot.test.web.client.TestRestTemplate;
23-
import org.springframework.context.ApplicationContext;
2423
import org.springframework.http.HttpStatus;
2524
import org.springframework.http.ResponseEntity;
2625
import org.springframework.test.context.junit4.SpringRunner;
@@ -30,16 +29,17 @@
3029
import static org.hamcrest.CoreMatchers.containsString;
3130
import static org.hamcrest.CoreMatchers.not;
3231
import static org.junit.Assert.*;
33-
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
32+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
3433

3534
/**
3635
* Created by alexf on 28-Jan-16.
3736
*/
3837
@RunWith(SpringRunner.class)
39-
@SpringBootTest(classes = {DemoApp.class,TestConfig.class}, webEnvironment = DEFINED_PORT)
38+
@SpringBootTest(classes = {DemoApp.class,TestConfig.class}, webEnvironment = RANDOM_PORT)
4039
public class DemoAppTest extends GrpcServerTestBase{
4140

42-
41+
@LocalServerPort
42+
private int localServerPort;
4343

4444
@Rule
4545
public OutputCapture outputCapture = new OutputCapture();
@@ -49,14 +49,10 @@ public class DemoAppTest extends GrpcServerTestBase{
4949
private ServerInterceptor globalInterceptor;
5050

5151

52-
53-
54-
5552
@Test
5653
public void disabledServerTest() throws Throwable {
5754
assertNotNull(grpcServerRunner);
5855
assertNull(grpcInprocessServerRunner);
59-
6056
}
6157

6258
@Test
@@ -80,16 +76,13 @@ public void interceptorsTest() throws ExecutionException, InterruptedException {
8076

8177

8278
outputCapture.expect(containsString("I'm not Spring bean interceptor and still being invoked..."));
83-
84-
85-
8679
}
8780

8881
@Test
8982
public void actuatorTest() throws ExecutionException, InterruptedException {
9083
final TestRestTemplate template = new TestRestTemplate();
9184

92-
ResponseEntity<String> response = template.getForEntity("http://localhost:8080/env", String.class);
85+
ResponseEntity<String> response = template.getForEntity("http://localhost:" + localServerPort + "/env", String.class);
9386
assertEquals(HttpStatus.OK, response.getStatusCode());
9487
}
9588

0 commit comments

Comments
 (0)