@@ -78,7 +78,7 @@ public void testSetGetUsername() {
78
78
}
79
79
80
80
@ Test
81
- public void testIsReachable () throws IOException , InterruptedException , URISyntaxException {
81
+ public void testIsReachablePositive () throws IOException , InterruptedException , URISyntaxException {
82
82
// Start simple TCP server on test port.
83
83
InetAddress localhostAddr = InetAddress .getLoopbackAddress ();
84
84
try (ServerSocket serverSocket = new ServerSocket (0 , 1 )) {
@@ -115,17 +115,27 @@ public void testIsReachable() throws IOException, InterruptedException, URISynta
115
115
116
116
// Test reachability.
117
117
boolean reachable = serverSpy .isReachable ();
118
- serverSocket .close ();
119
- thread .join (5000 );
120
- thread .interrupt ();
121
118
assertTrue (reachable );
122
119
123
- // Test non-reachability.
124
- reachable = serverSpy .isReachable ();
125
- assertFalse (reachable );
120
+ thread .interrupt ();
121
+ thread .join (5000 );
126
122
}
127
123
}
128
124
125
+ @ Test
126
+ void testsReachableNegative () throws Exception {
127
+ InetAddress localhostAddr = InetAddress .getLoopbackAddress ();
128
+
129
+ // Mock getAddresses() to return single localhost IP address and getPort() to return the test port.
130
+ LdapServer server = new LdapServer ("ldaps://foo.bar.com" );
131
+ LdapServer serverSpy = Mockito .spy (server );
132
+ Mockito .when (serverSpy .getAddresses (any ())).thenReturn (new InetAddress []{localhostAddr });
133
+ // port 0 should not be reachable.
134
+ doReturn (0 ).when (serverSpy ).getPort ();
135
+
136
+ assertFalse (serverSpy .isReachable ());
137
+ }
138
+
129
139
@ Test
130
140
public void testEmptyAddressArray () throws UnknownHostException {
131
141
LdapServer server = new LdapServer ("ldaps://foo.bar.com" );
0 commit comments