Skip to content

Commit 62b2f5c

Browse files
mocked tests that were inconsistent on GitHub Actions VM runs #2
1 parent db446bd commit 62b2f5c

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieImplAddressMockTest.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,30 @@
22

33
import org.apache.bookkeeper.bookie.util.TestAddressUtil;
44
import org.apache.bookkeeper.bookie.util.TestBKConfiguration;
5+
import org.apache.bookkeeper.bookie.util.testtypes.CustomBookieSocketAddress;
56
import org.apache.bookkeeper.conf.ServerConfiguration;
67
import org.apache.bookkeeper.net.BookieSocketAddress;
78
import org.apache.bookkeeper.net.DNS;
89
import org.junit.Test;
910
import org.junit.runner.RunWith;
11+
import org.mockito.stubbing.Answer;
1012
import org.powermock.api.mockito.PowerMockito;
1113
import org.powermock.core.classloader.annotations.PowerMockIgnore;
1214
import org.powermock.core.classloader.annotations.PrepareForTest;
1315
import org.powermock.modules.junit4.PowerMockRunner;
1416

15-
import java.net.*;
17+
import java.lang.reflect.Method;
18+
import java.net.Inet4Address;
19+
import java.net.InetAddress;
20+
import java.net.NetworkInterface;
21+
import java.net.UnknownHostException;
1622

1723
import static org.apache.bookkeeper.bookie.util.TestAddressUtil.getInterfaceName;
1824
import static org.junit.jupiter.api.Assertions.assertEquals;
1925
import static org.junit.jupiter.api.Assertions.assertTrue;
20-
import static org.powermock.api.mockito.PowerMockito.*;
26+
import static org.powermock.api.mockito.PowerMockito.spy;
2127

22-
@PrepareForTest({DNS.class, Inet4Address.class})
28+
@PrepareForTest({DNS.class})
2329
@RunWith(PowerMockRunner.class)
2430
@PowerMockIgnore("javax.management.*")
2531
public class BookieImplAddressMockTest {
@@ -38,8 +44,8 @@ public BookieImplAddressMockTest() throws UnknownHostException {
3844
@Test
3945
public void testUnknownInterface() throws Exception {
4046

41-
spy(DNS.class);
42-
PowerMockito.when(DNS.class, "getSubinterface", "notAnInterface").thenReturn(null);
47+
PowerMockito.spy(DNS.class);
48+
PowerMockito.when(DNS.class, "getSubinterface", "notAnInterface").thenAnswer((Answer<NetworkInterface>) invocation -> null);
4349

4450
ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
4551
conf.setAdvertisedAddress("");
@@ -63,12 +69,12 @@ public void testUnknownInterface() throws Exception {
6369
}
6470

6571
@Test
66-
public void testLoopBackException() throws SocketException, UnknownHostException {
72+
public void testLoopBackException() throws Exception {
73+
74+
75+
BookieSocketAddress myBookie = new CustomBookieSocketAddress(InetAddress.getLocalHost().getCanonicalHostName(), 0);
76+
PowerMockito.whenNew(BookieSocketAddress.class).withAnyArguments().thenReturn(myBookie);
6777

68-
InetSocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getLocalHost().getHostName(), 0);
69-
InetAddress socketAddress = inetSocketAddress.getAddress();
70-
InetAddress mockedAddress = mock(socketAddress.getClass());
71-
when(mockedAddress.isLoopbackAddress()).thenReturn(true);
7278

7379
ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
7480
conf.setAdvertisedAddress("");
@@ -85,7 +91,7 @@ public void testLoopBackException() throws SocketException, UnknownHostException
8591
} catch (UnknownHostException e) {
8692
exceptionThrown = true;
8793

88-
assertEquals(LB_MSG, e.getMessage());
94+
assertEquals(LB_MSG, e.getMessage());
8995
}
9096

9197
assertTrue(exceptionThrown);
@@ -97,4 +103,7 @@ public void testLoopBackException() throws SocketException, UnknownHostException
97103

98104

99105

100-
}
106+
107+
108+
109+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.apache.bookkeeper.bookie.util.testtypes;
2+
3+
import org.apache.bookkeeper.net.BookieSocketAddress;
4+
5+
import java.net.InetSocketAddress;
6+
7+
public class CustomBookieSocketAddress extends BookieSocketAddress {
8+
9+
10+
private InetSocketAddress loopbackSocketAddress;
11+
public CustomBookieSocketAddress(String hostname, int port) {
12+
super(hostname, port);
13+
this.setUpMockedAddress();
14+
15+
}
16+
17+
private void setUpMockedAddress() {
18+
loopbackSocketAddress = new InetSocketAddress("localhost", 8080);
19+
if (!loopbackSocketAddress.getAddress().isLoopbackAddress()) {
20+
System.out.println("****ADDRESS NON LOOPBACK****");
21+
throw new RuntimeException();
22+
}
23+
}
24+
25+
@Override
26+
public InetSocketAddress getSocketAddress() {
27+
28+
return this.loopbackSocketAddress;
29+
30+
}
31+
}
32+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mock-maker-inline

0 commit comments

Comments
 (0)