11package org .lognet .springboot .grpc ;
22
3- import io .grpc .ManagedChannel ;
4- import io .grpc .ManagedChannelBuilder ;
53import io .grpc .ServerInterceptor ;
64import io .grpc .examples .CalculatorGrpc ;
75import io .grpc .examples .CalculatorOuterClass ;
108import io .grpc .health .v1 .HealthCheckRequest ;
119import io .grpc .health .v1 .HealthCheckResponse ;
1210import 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 ;
1514import org .junit .runner .RunWith ;
1615import org .lognet .springboot .grpc .demo .DemoApp ;
1716import org .mockito .Mockito ;
1817import org .springframework .beans .factory .annotation .Autowired ;
1918import org .springframework .beans .factory .annotation .Qualifier ;
19+ import org .springframework .boot .context .embedded .LocalServerPort ;
2020import org .springframework .boot .test .context .SpringBootTest ;
2121import org .springframework .boot .test .rule .OutputCapture ;
2222import org .springframework .boot .test .web .client .TestRestTemplate ;
23- import org .springframework .context .ApplicationContext ;
2423import org .springframework .http .HttpStatus ;
2524import org .springframework .http .ResponseEntity ;
2625import org .springframework .test .context .junit4 .SpringRunner ;
3029import static org .hamcrest .CoreMatchers .containsString ;
3130import static org .hamcrest .CoreMatchers .not ;
3231import 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 )
4039public 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