2323import org .junit .Test ;
2424import org .junit .runner .RunWith ;
2525import org .springframework .beans .factory .annotation .Autowired ;
26+ import org .springframework .beans .factory .annotation .Value ;
2627import org .springframework .boot .autoconfigure .security .SecurityProperties ;
2728import org .springframework .boot .test .IntegrationTest ;
2829import org .springframework .boot .test .SpringApplicationConfiguration ;
5051@ RunWith (SpringJUnit4ClassRunner .class )
5152@ SpringApplicationConfiguration (classes = SampleActuatorApplication .class )
5253@ WebAppConfiguration
53- @ IntegrationTest
54+ @ IntegrationTest ( "server.port=0" )
5455@ DirtiesContext
5556public class SampleActuatorApplicationTests {
5657
5758@ Autowired
5859private SecurityProperties security ;
5960
61+ @ Value ("${local.server.port}" )
62+ private int port ;
63+
6064@ Test
6165public void testHomeIsSecure () throws Exception {
6266@ SuppressWarnings ("rawtypes" )
6367ResponseEntity <Map > entity = new TestRestTemplate ().getForEntity (
64- "http://localhost:8080" , Map .class );
68+ "http://localhost:" + port , Map .class );
6569assertEquals (HttpStatus .UNAUTHORIZED , entity .getStatusCode ());
6670@ SuppressWarnings ("unchecked" )
6771Map <String , Object > body = entity .getBody ();
@@ -74,24 +78,24 @@ public void testHomeIsSecure() throws Exception {
7478public void testMetricsIsSecure () throws Exception {
7579@ SuppressWarnings ("rawtypes" )
7680ResponseEntity <Map > entity = new TestRestTemplate ().getForEntity (
77- "http://localhost:8080 /metrics" , Map .class );
81+ "http://localhost:" + port + " /metrics" , Map .class );
7882assertEquals (HttpStatus .UNAUTHORIZED , entity .getStatusCode ());
79- entity = new TestRestTemplate ().getForEntity (
80- "http://localhost:8080/metrics/" , Map .class );
83+ entity = new TestRestTemplate ().getForEntity ("http://localhost:" + port + "/metrics/" ,
84+ Map .class );
8185assertEquals (HttpStatus .UNAUTHORIZED , entity .getStatusCode ());
82- entity = new TestRestTemplate ().getForEntity (
83- "http://localhost:8080/metrics/foo" , Map .class );
86+ entity = new TestRestTemplate ().getForEntity ("http://localhost:" + port + "/metrics/foo" ,
87+ Map .class );
8488assertEquals (HttpStatus .UNAUTHORIZED , entity .getStatusCode ());
8589entity = new TestRestTemplate ().getForEntity (
86- "http://localhost:8080 /metrics.json" , Map .class );
90+ "http://localhost:" + port + " /metrics.json" , Map .class );
8791assertEquals (HttpStatus .UNAUTHORIZED , entity .getStatusCode ());
8892}
8993
9094@ Test
9195public void testHome () throws Exception {
9296@ SuppressWarnings ("rawtypes" )
9397ResponseEntity <Map > entity = new TestRestTemplate ("user" , getPassword ())
94- .getForEntity ("http://localhost:8080" , Map .class );
98+ .getForEntity ("http://localhost:" + port , Map .class );
9599assertEquals (HttpStatus .OK , entity .getStatusCode ());
96100@ SuppressWarnings ("unchecked" )
97101Map <String , Object > body = entity .getBody ();
@@ -103,7 +107,7 @@ public void testMetrics() throws Exception {
103107testHome (); // makes sure some requests have been made
104108@ SuppressWarnings ("rawtypes" )
105109ResponseEntity <Map > entity = new TestRestTemplate ("user" , getPassword ())
106- .getForEntity ("http://localhost:8080 /metrics" , Map .class );
110+ .getForEntity ("http://localhost:" + port + " /metrics" , Map .class );
107111assertEquals (HttpStatus .OK , entity .getStatusCode ());
108112@ SuppressWarnings ("unchecked" )
109113Map <String , Object > body = entity .getBody ();
@@ -114,7 +118,7 @@ public void testMetrics() throws Exception {
114118public void testEnv () throws Exception {
115119@ SuppressWarnings ("rawtypes" )
116120ResponseEntity <Map > entity = new TestRestTemplate ("user" , getPassword ())
117- .getForEntity ("http://localhost:8080 /env" , Map .class );
121+ .getForEntity ("http://localhost:" + port + " /env" , Map .class );
118122assertEquals (HttpStatus .OK , entity .getStatusCode ());
119123@ SuppressWarnings ("unchecked" )
120124Map <String , Object > body = entity .getBody ();
@@ -124,15 +128,15 @@ public void testEnv() throws Exception {
124128@ Test
125129public void testHealth () throws Exception {
126130ResponseEntity <String > entity = new TestRestTemplate ().getForEntity (
127- "http://localhost:8080 /health" , String .class );
131+ "http://localhost:" + port + " /health" , String .class );
128132assertEquals (HttpStatus .OK , entity .getStatusCode ());
129133assertEquals ("ok" , entity .getBody ());
130134}
131135
132136@ Test
133137public void testErrorPage () throws Exception {
134138ResponseEntity <String > entity = new TestRestTemplate ("user" , getPassword ())
135- .getForEntity ("http://localhost:8080 /foo" , String .class );
139+ .getForEntity ("http://localhost:" + port + " /foo" , String .class );
136140assertEquals (HttpStatus .INTERNAL_SERVER_ERROR , entity .getStatusCode ());
137141String body = entity .getBody ();
138142assertNotNull (body );
@@ -145,7 +149,7 @@ public void testHtmlErrorPage() throws Exception {
145149headers .setAccept (Arrays .asList (MediaType .TEXT_HTML ));
146150HttpEntity <?> request = new HttpEntity <Void >(headers );
147151ResponseEntity <String > entity = new TestRestTemplate ("user" , getPassword ())
148- .exchange ("http://localhost:8080 /foo" , HttpMethod .GET , request ,
152+ .exchange ("http://localhost:" + port + " /foo" , HttpMethod .GET , request ,
149153String .class );
150154assertEquals (HttpStatus .INTERNAL_SERVER_ERROR , entity .getStatusCode ());
151155String body = entity .getBody ();
@@ -156,10 +160,10 @@ public void testHtmlErrorPage() throws Exception {
156160
157161@ Test
158162public void testTrace () throws Exception {
159- new TestRestTemplate ().getForEntity ("http://localhost:8080 /health" , String .class );
163+ new TestRestTemplate ().getForEntity ("http://localhost:" + port + " /health" , String .class );
160164@ SuppressWarnings ("rawtypes" )
161165ResponseEntity <List > entity = new TestRestTemplate ("user" , getPassword ())
162- .getForEntity ("http://localhost:8080 /trace" , List .class );
166+ .getForEntity ("http://localhost:" + port + " /trace" , List .class );
163167assertEquals (HttpStatus .OK , entity .getStatusCode ());
164168@ SuppressWarnings ("unchecked" )
165169List <Map <String , Object >> list = entity .getBody ();
@@ -174,7 +178,7 @@ public void testTrace() throws Exception {
174178public void testErrorPageDirectAccess () throws Exception {
175179@ SuppressWarnings ("rawtypes" )
176180ResponseEntity <Map > entity = new TestRestTemplate ().getForEntity (
177- "http://localhost:8080 /error" , Map .class );
181+ "http://localhost:" + port + " /error" , Map .class );
178182assertEquals (HttpStatus .INTERNAL_SERVER_ERROR , entity .getStatusCode ());
179183@ SuppressWarnings ("unchecked" )
180184Map <String , Object > body = entity .getBody ();
@@ -186,7 +190,7 @@ public void testErrorPageDirectAccess() throws Exception {
186190public void testBeans () throws Exception {
187191@ SuppressWarnings ("rawtypes" )
188192ResponseEntity <List > entity = new TestRestTemplate ("user" , getPassword ())
189- .getForEntity ("http://localhost:8080 /beans" , List .class );
193+ .getForEntity ("http://localhost:" + port + " /beans" , List .class );
190194assertEquals (HttpStatus .OK , entity .getStatusCode ());
191195assertEquals (1 , entity .getBody ().size ());
192196@ SuppressWarnings ("unchecked" )
0 commit comments