|
1 | 1 | /*
|
2 |
| - * Copyright 2013-2019 the original author or authors. |
| 2 | + * Copyright 2013-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
27 | 27 | import io.fabric8.kubernetes.client.dsl.Resource;
|
28 | 28 | import org.junit.jupiter.api.BeforeEach;
|
29 | 29 | import org.junit.jupiter.api.Test;
|
30 |
| -import org.junit.jupiter.api.extension.ExtendWith; |
31 |
| -import org.mockito.Mock; |
32 |
| -import org.mockito.junit.jupiter.MockitoExtension; |
| 30 | +import org.mockito.Mockito; |
33 | 31 |
|
34 | 32 | import org.springframework.cloud.kubernetes.commons.leader.LeaderProperties;
|
35 | 33 |
|
36 |
| -import static org.mockito.BDDMockito.given; |
37 | 34 | import static org.mockito.Mockito.times;
|
38 | 35 | import static org.mockito.Mockito.verify;
|
39 | 36 |
|
40 | 37 | /**
|
41 | 38 | * @author Gytis Trikleris
|
42 | 39 | */
|
43 |
| -@ExtendWith(MockitoExtension.class) |
44 |
| -public class Fabric8LeaderRecordWatcherTest { |
| 40 | +class Fabric8LeaderRecordWatcherTest { |
45 | 41 |
|
46 |
| - @Mock |
47 |
| - private LeaderProperties mockLeaderProperties; |
| 42 | + private final LeaderProperties mockLeaderProperties = Mockito.mock(LeaderProperties.class); |
48 | 43 |
|
49 |
| - @Mock |
50 |
| - private Fabric8LeadershipController mockFabric8LeadershipController; |
| 44 | + private final Fabric8LeadershipController mockFabric8LeadershipController = Mockito |
| 45 | + .mock(Fabric8LeadershipController.class); |
51 | 46 |
|
52 |
| - @Mock |
53 |
| - private KubernetesClient mockKubernetesClient; |
| 47 | + private final KubernetesClient mockKubernetesClient = Mockito.mock(KubernetesClient.class); |
54 | 48 |
|
55 |
| - @Mock |
56 |
| - private MixedOperation<ConfigMap, ConfigMapList, Resource<ConfigMap>> mockConfigMapsOperation; |
| 49 | + @SuppressWarnings("unchecked") |
| 50 | + private final MixedOperation<ConfigMap, ConfigMapList, Resource<ConfigMap>> mockConfigMapsOperation = Mockito |
| 51 | + .mock(MixedOperation.class); |
57 | 52 |
|
58 |
| - @Mock |
59 |
| - private NonNamespaceOperation<ConfigMap, ConfigMapList, Resource<ConfigMap>> mockInNamespaceOperation; |
| 53 | + @SuppressWarnings("unchecked") |
| 54 | + private final NonNamespaceOperation<ConfigMap, ConfigMapList, Resource<ConfigMap>> mockInNamespaceOperation = Mockito |
| 55 | + .mock(NonNamespaceOperation.class); |
60 | 56 |
|
61 |
| - @Mock |
62 |
| - private Resource<ConfigMap> mockWithNameResource; |
| 57 | + @SuppressWarnings("unchecked") |
| 58 | + private final Resource<ConfigMap> mockWithNameResource = Mockito.mock(Resource.class); |
63 | 59 |
|
64 |
| - @Mock |
65 |
| - private Watch mockWatch; |
| 60 | + private final Watch mockWatch = Mockito.mock(Watch.class); |
66 | 61 |
|
67 |
| - @Mock |
68 |
| - private ConfigMap mockConfigMap; |
| 62 | + private final ConfigMap mockConfigMap = Mockito.mock(ConfigMap.class); |
69 | 63 |
|
70 |
| - @Mock |
71 |
| - private WatcherException mockKubernetesClientException; |
| 64 | + private final WatcherException mockKubernetesClientException = Mockito.mock(WatcherException.class); |
72 | 65 |
|
73 | 66 | private Fabric8LeaderRecordWatcher watcher;
|
74 | 67 |
|
75 | 68 | @BeforeEach
|
76 |
| - public void before() { |
77 |
| - this.watcher = new Fabric8LeaderRecordWatcher(this.mockLeaderProperties, this.mockFabric8LeadershipController, |
78 |
| - this.mockKubernetesClient); |
| 69 | + void beforeEach() { |
| 70 | + watcher = new Fabric8LeaderRecordWatcher(mockLeaderProperties, mockFabric8LeadershipController, |
| 71 | + mockKubernetesClient); |
79 | 72 | }
|
80 | 73 |
|
81 | 74 | @Test
|
82 |
| - public void shouldStartOnce() { |
| 75 | + void shouldStartOnce() { |
83 | 76 | initStubs();
|
84 |
| - this.watcher.start(); |
85 |
| - this.watcher.start(); |
86 |
| - |
87 |
| - verify(this.mockWithNameResource).watch(this.watcher); |
| 77 | + watcher.start(); |
| 78 | + watcher.start(); |
| 79 | + verify(mockWithNameResource).watch(watcher); |
88 | 80 | }
|
89 | 81 |
|
90 | 82 | @Test
|
91 |
| - public void shouldStopOnce() { |
| 83 | + void shouldStopOnce() { |
92 | 84 | initStubs();
|
93 |
| - this.watcher.start(); |
94 |
| - this.watcher.stop(); |
95 |
| - this.watcher.stop(); |
96 |
| - |
97 |
| - verify(this.mockWatch).close(); |
| 85 | + watcher.start(); |
| 86 | + watcher.stop(); |
| 87 | + watcher.stop(); |
| 88 | + verify(mockWatch).close(); |
98 | 89 | }
|
99 | 90 |
|
100 | 91 | @Test
|
101 |
| - public void shouldHandleEvent() { |
102 |
| - this.watcher.eventReceived(Watcher.Action.ADDED, this.mockConfigMap); |
103 |
| - this.watcher.eventReceived(Watcher.Action.DELETED, this.mockConfigMap); |
104 |
| - this.watcher.eventReceived(Watcher.Action.MODIFIED, this.mockConfigMap); |
105 |
| - |
106 |
| - verify(this.mockFabric8LeadershipController, times(3)).update(); |
| 92 | + void shouldHandleEvent() { |
| 93 | + watcher.eventReceived(Watcher.Action.ADDED, mockConfigMap); |
| 94 | + watcher.eventReceived(Watcher.Action.DELETED, mockConfigMap); |
| 95 | + watcher.eventReceived(Watcher.Action.MODIFIED, mockConfigMap); |
| 96 | + verify(mockFabric8LeadershipController, times(3)).update(); |
107 | 97 | }
|
108 | 98 |
|
109 | 99 | @Test
|
110 |
| - public void shouldIgnoreErrorEvent() { |
111 |
| - this.watcher.eventReceived(Watcher.Action.ERROR, this.mockConfigMap); |
112 |
| - |
113 |
| - verify(this.mockFabric8LeadershipController, times(0)).update(); |
| 100 | + void shouldIgnoreErrorEvent() { |
| 101 | + watcher.eventReceived(Watcher.Action.ERROR, mockConfigMap); |
| 102 | + verify(mockFabric8LeadershipController, times(0)).update(); |
114 | 103 | }
|
115 | 104 |
|
116 | 105 | @Test
|
117 |
| - public void shouldHandleClose() { |
| 106 | + void shouldHandleClose() { |
118 | 107 | initStubs();
|
119 |
| - this.watcher.onClose(this.mockKubernetesClientException); |
120 |
| - |
121 |
| - verify(this.mockWithNameResource).watch(this.watcher); |
| 108 | + watcher.onClose(mockKubernetesClientException); |
| 109 | + verify(mockWithNameResource).watch(watcher); |
122 | 110 | }
|
123 | 111 |
|
124 | 112 | @Test
|
125 |
| - public void shouldIgnoreCloseWithoutCause() { |
126 |
| - this.watcher.onClose(null); |
127 |
| - |
128 |
| - verify(this.mockWithNameResource, times(0)).watch(this.watcher); |
| 113 | + void shouldIgnoreCloseWithoutCause() { |
| 114 | + watcher.onClose(null); |
| 115 | + verify(mockWithNameResource, times(0)).watch(watcher); |
129 | 116 | }
|
130 | 117 |
|
131 | 118 | private void initStubs() {
|
132 |
| - given(this.mockKubernetesClient.configMaps()).willReturn(this.mockConfigMapsOperation); |
133 |
| - given(this.mockConfigMapsOperation.inNamespace(null)).willReturn(this.mockInNamespaceOperation); |
134 |
| - given(this.mockInNamespaceOperation.withName(null)).willReturn(this.mockWithNameResource); |
135 |
| - given(this.mockWithNameResource.watch(this.watcher)).willReturn(this.mockWatch); |
| 119 | + Mockito.when(mockKubernetesClient.configMaps()).thenReturn(mockConfigMapsOperation); |
| 120 | + Mockito.when(mockConfigMapsOperation.inNamespace(null)).thenReturn(mockInNamespaceOperation); |
| 121 | + Mockito.when(mockInNamespaceOperation.withName(null)).thenReturn(mockWithNameResource); |
| 122 | + Mockito.when(mockWithNameResource.watch(watcher)).thenReturn(mockWatch); |
136 | 123 | }
|
137 | 124 |
|
138 | 125 | }
|
0 commit comments