3333 * @author Eric Martin
3434 */
3535@ RunWith (PowerMockRunner .class )
36- @ PrepareForTest ({EnvironmentDetails .class , InetAddress .class })
36+ @ PrepareForTest ({EnvironmentDetails .class , System . class , InetAddress .class })
3737public class EnvironmentDetailsTest {
3838
3939 /**
@@ -45,12 +45,10 @@ public void testGetEnvironmentDetail() throws UnknownHostException {
4545 String application = "application" ;
4646 String environment = "environment" ;
4747 String hostName = "hostName" ;
48-
49- InetAddress inetAddress = PowerMockito .mock (InetAddress .class );
50- PowerMockito .when (inetAddress .getHostName ()).thenReturn (hostName );
51-
52- PowerMockito .mockStatic (InetAddress .class );
53- PowerMockito .when (InetAddress .getLocalHost ()).thenReturn (inetAddress );
48+
49+ PowerMockito .mockStatic (System .class );
50+ PowerMockito .when (System .getenv ("HOSTNAME" )).thenReturn (hostName );
51+ PowerMockito .when (System .getProperty ("user.dir" )).thenReturn ("/some/dir/" );
5452
5553 EnvironmentDetail env = EnvironmentDetails .getEnvironmentDetail (application , environment );
5654
@@ -64,22 +62,30 @@ public void testGetEnvironmentDetail() throws UnknownHostException {
6462 }
6563
6664 /**
67- * testGetEnvironmentDetailHostnameException
65+ * testGetEnvironmentDetailWithoutHostnameEnvVar
6866 * @throws UnknownHostException
6967 */
7068 @ Test
71- public void testGetEnvironmentDetailHostnameException () throws UnknownHostException {
69+ public void testGetEnvironmentDetailWithoutHostnameEnvVar () throws UnknownHostException {
7270 String application = "application" ;
7371 String environment = "environment" ;
74-
72+ String hostName = "hostName" ;
73+
74+ PowerMockito .mockStatic (System .class );
75+ PowerMockito .when (System .getenv ("HOSTNAME" )).thenReturn (null );
76+ PowerMockito .when (System .getProperty ("user.dir" )).thenReturn ("/some/dir/" );
77+
78+ InetAddress inetAddress = PowerMockito .mock (InetAddress .class );
79+ PowerMockito .when (inetAddress .getHostName ()).thenReturn (hostName );
80+
7581 PowerMockito .mockStatic (InetAddress .class );
76- PowerMockito .when (InetAddress .getLocalHost ()).thenThrow ( new RuntimeException () );
82+ PowerMockito .when (InetAddress .getLocalHost ()).thenReturn ( inetAddress );
7783
7884 EnvironmentDetail env = EnvironmentDetails .getEnvironmentDetail (application , environment );
7985
8086 Assert .assertNotNull (env );
8187
82- Assert .assertNull ( env .getDeviceName ());
88+ Assert .assertEquals ( hostName , env .getDeviceName ());
8389 Assert .assertNotNull (env .getAppLocation ());
8490 Assert .assertNull (env .getAppName ());
8591 Assert .assertEquals (application , env .getConfiguredAppName ());
0 commit comments