Skip to content

Commit 39203b1

Browse files
kohlschuetterkofemann
authored andcommitted
test: Fix tests on Java 21 or newer; replace InetAddress mocking code
Some unit tests work with fake ("mocked") InetAddress objects, which are currently assembled via mockito. With Java 21 and newer, this is no longer possible. Replace the mocking code with direct calls to InetAddress.getByAddress(String,byte[]). Fixes #144 Signed-off-by: Christian Kohlschütter <christian@kohlschutter.com> (cherry picked from commit 7d7a190) Signed-off-by: Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
1 parent 7a3c864 commit 39203b1

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

core/src/test/java/org/dcache/nfs/InetAddressMatcherTest.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@
1919
*/
2020
package org.dcache.nfs;
2121

22-
import java.net.UnknownHostException;
22+
import static com.google.common.net.InetAddresses.forString;
23+
import static org.junit.Assert.assertEquals;
24+
import static org.junit.Assert.assertFalse;
25+
import static org.junit.Assert.assertTrue;
26+
2327
import java.net.InetAddress;
24-
import org.junit.Test;
28+
import java.net.UnknownHostException;
2529

26-
import static com.google.common.net.InetAddresses.forString;
27-
import static org.mockito.Mockito.*;
28-
import static org.mockito.BDDMockito.given;
29-
import static org.junit.Assert.*;
3030
import org.junit.Ignore;
31+
import org.junit.Test;
3132

3233
public class InetAddressMatcherTest {
3334

@@ -162,12 +163,7 @@ public void testDomainNoMatch() throws UnknownHostException {
162163
assertFalse("incorrect host matched by domain", ipMatcher.match(addr));
163164
}
164165

165-
private InetAddress mockInetAddress(String dnsName, String...ips) throws UnknownHostException {
166-
167-
InetAddress mockedAddress = mock(InetAddress.class);
168-
given(mockedAddress.getHostName()).willReturn(dnsName);
169-
given(mockedAddress.getCanonicalHostName()).willReturn(dnsName);
170-
171-
return mockedAddress;
166+
private InetAddress mockInetAddress(String dnsName, String... ips) throws UnknownHostException {
167+
return InetAddress.getByAddress(dnsName, InetAddress.getByName(ips[0]).getAddress());
172168
}
173169
}

core/src/test/java/org/dcache/testutils/InetAddressBuilder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.dcache.testutils;
22

33
import java.net.InetAddress;
4-
5-
import org.mockito.Mockito;
4+
import java.net.UnknownHostException;
65

76
public class InetAddressBuilder {
87
private String ipAddress;
@@ -19,9 +18,10 @@ public InetAddressBuilder hostName(String hostName) {
1918
}
2019

2120
public InetAddress build() {
22-
InetAddress address = Mockito.mock(InetAddress.class);
23-
Mockito.when(address.getHostAddress()).thenReturn(ipAddress);
24-
Mockito.when(address.getHostName()).thenReturn(hostName);
25-
return address;
21+
try {
22+
return InetAddress.getByAddress(hostName, InetAddress.getByName(ipAddress).getAddress());
23+
} catch (UnknownHostException e) {
24+
throw new IllegalStateException(e);
25+
}
2626
}
2727
}

0 commit comments

Comments
 (0)