55import io .grpc .ManagedChannelBuilder ;
66import io .grpc .examples .GreeterGrpc ;
77import io .grpc .examples .GreeterOuterClass ;
8+ import org .junit .AfterClass ;
89import org .junit .ClassRule ;
910import org .junit .Test ;
1011import org .junit .runner .RunWith ;
1415import org .springframework .cloud .client .ServiceInstance ;
1516import org .springframework .cloud .client .discovery .DiscoveryClient ;
1617import org .springframework .test .context .junit4 .SpringRunner ;
18+ import org .springframework .util .SocketUtils ;
1719
1820import java .util .List ;
1921import java .util .concurrent .ExecutionException ;
2729public class ConsulRegistrationTest {
2830
2931 @ ClassRule
30- public static final ConsulResource consul = new ConsulResource (8500 );
32+ public static final ConsulResource consul (){
33+ int port = SocketUtils .findAvailableTcpPort ();
34+ ConsulResource consulResource = new ConsulResource (port );
35+ System .setProperty ("spring.cloud.consul.port" ,String .valueOf (port ));
36+ return consulResource ;
37+ }
38+ @ AfterClass
39+ public static void clear (){
40+ System .clearProperty ("spring.cloud.consul.port" );
41+ }
3142
3243 @ Autowired
3344 private DiscoveryClient discoveryClient ;
@@ -46,7 +57,5 @@ public void contextLoads() throws ExecutionException, InterruptedException {
4657 final GreeterOuterClass .HelloRequest helloRequest =GreeterOuterClass .HelloRequest .newBuilder ().setName ("Bob" ).build ();
4758 final String reply = greeterFutureStub .sayHello (helloRequest ).get ().getMessage ();
4859 assertNotNull ("Replay should not be null" ,reply );
49-
50-
5160 }
5261}
0 commit comments