2
2
3
3
import org .apache .bookkeeper .bookie .util .TestAddressUtil ;
4
4
import org .apache .bookkeeper .bookie .util .TestBKConfiguration ;
5
+ import org .apache .bookkeeper .bookie .util .testtypes .CustomBookieSocketAddress ;
5
6
import org .apache .bookkeeper .conf .ServerConfiguration ;
6
7
import org .apache .bookkeeper .net .BookieSocketAddress ;
7
8
import org .apache .bookkeeper .net .DNS ;
8
9
import org .junit .Test ;
9
10
import org .junit .runner .RunWith ;
11
+ import org .mockito .stubbing .Answer ;
10
12
import org .powermock .api .mockito .PowerMockito ;
11
13
import org .powermock .core .classloader .annotations .PowerMockIgnore ;
12
14
import org .powermock .core .classloader .annotations .PrepareForTest ;
13
15
import org .powermock .modules .junit4 .PowerMockRunner ;
14
16
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 ;
16
22
17
23
import static org .apache .bookkeeper .bookie .util .TestAddressUtil .getInterfaceName ;
18
24
import static org .junit .jupiter .api .Assertions .assertEquals ;
19
25
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 ;
21
27
22
- @ PrepareForTest ({DNS .class , Inet4Address . class })
28
+ @ PrepareForTest ({DNS .class })
23
29
@ RunWith (PowerMockRunner .class )
24
30
@ PowerMockIgnore ("javax.management.*" )
25
31
public class BookieImplAddressMockTest {
@@ -38,8 +44,8 @@ public BookieImplAddressMockTest() throws UnknownHostException {
38
44
@ Test
39
45
public void testUnknownInterface () throws Exception {
40
46
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 );
43
49
44
50
ServerConfiguration conf = TestBKConfiguration .newServerConfiguration ();
45
51
conf .setAdvertisedAddress ("" );
@@ -63,12 +69,12 @@ public void testUnknownInterface() throws Exception {
63
69
}
64
70
65
71
@ 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 );
67
77
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 );
72
78
73
79
ServerConfiguration conf = TestBKConfiguration .newServerConfiguration ();
74
80
conf .setAdvertisedAddress ("" );
@@ -85,7 +91,7 @@ public void testLoopBackException() throws SocketException, UnknownHostException
85
91
} catch (UnknownHostException e ) {
86
92
exceptionThrown = true ;
87
93
88
- assertEquals (LB_MSG , e .getMessage ());
94
+ assertEquals (LB_MSG , e .getMessage ());
89
95
}
90
96
91
97
assertTrue (exceptionThrown );
@@ -97,4 +103,7 @@ public void testLoopBackException() throws SocketException, UnknownHostException
97
103
98
104
99
105
100
- }
106
+
107
+
108
+
109
+ }
0 commit comments