|
32 | 32 | import org.junit.jupiter.api.Test; |
33 | 33 | import org.junit.jupiter.api.extension.ExtendWith; |
34 | 34 | import org.mockito.Mock; |
35 | | -import org.mockito.MockitoAnnotations; |
| 35 | +import org.mockito.MockedStatic; |
36 | 36 | import org.mockito.junit.jupiter.MockitoExtension; |
37 | 37 | import org.springframework.context.ApplicationContext; |
| 38 | +import org.springframework.context.event.ContextRefreshedEvent; |
38 | 39 | import org.springframework.util.ReflectionUtils; |
39 | 40 |
|
40 | 41 | import java.lang.annotation.Annotation; |
|
49 | 50 | import static org.junit.jupiter.api.Assertions.assertNull; |
50 | 51 | import static org.mockito.ArgumentMatchers.any; |
51 | 52 | import static org.mockito.Mockito.mock; |
| 53 | +import static org.mockito.Mockito.mockStatic; |
52 | 54 | import static org.mockito.Mockito.never; |
| 55 | +import static org.mockito.Mockito.times; |
53 | 56 | import static org.mockito.Mockito.verify; |
54 | 57 | import static org.mockito.Mockito.when; |
55 | 58 |
|
@@ -84,21 +87,7 @@ public class SpringWebSocketClientEventListenerTest { |
84 | 87 |
|
85 | 88 | @BeforeEach |
86 | 89 | public void setUp() { |
87 | | - MockitoAnnotations.openMocks(this); |
88 | | - Properties properties = mock(Properties.class); |
89 | | - when(properties.getProperty("appName")).thenReturn("appName"); |
90 | | - when(properties.getProperty("contextPath")).thenReturn("contextPath"); |
91 | | - when(properties.getProperty(ShenyuClientConstants.PORT)).thenReturn("8080"); |
92 | | - when(properties.getProperty(ShenyuClientConstants.HOST)).thenReturn("127.0.0.1"); |
93 | | - when(properties.getProperty(ShenyuClientConstants.IP_PORT)).thenReturn("127.0.0.1:8080"); |
94 | | - |
95 | | - ShenyuClientConfig clientConfig = mock(ShenyuClientConfig.class); |
96 | | - Map<String, ClientPropertiesConfig> client = new HashMap<>(); |
97 | | - ClientPropertiesConfig clientPropertiesConfig = new ClientPropertiesConfig(); |
98 | | - clientPropertiesConfig.setProps(properties); |
99 | | - client.put(RpcTypeEnum.WEB_SOCKET.getName(), clientPropertiesConfig); |
100 | | - when(clientConfig.getClient()).thenReturn(client); |
101 | | - eventListener = new SpringWebSocketClientEventListener(clientConfig, registerRepository); |
| 90 | + eventListener = buildEventListener(false); |
102 | 91 | } |
103 | 92 |
|
104 | 93 | @Test |
@@ -178,6 +167,39 @@ public void testGetPort() { |
178 | 167 | assertEquals(port, "8080"); |
179 | 168 | } |
180 | 169 |
|
| 170 | + @Test |
| 171 | + public void testOnApplicationEventFullModeShouldRegisterOnce() { |
| 172 | + try (MockedStatic<ShenyuClientRegisterEventPublisher> publisherMockedStatic = mockStatic(ShenyuClientRegisterEventPublisher.class)) { |
| 173 | + ShenyuClientRegisterEventPublisher mockPublisher = mock(ShenyuClientRegisterEventPublisher.class); |
| 174 | + publisherMockedStatic.when(ShenyuClientRegisterEventPublisher::getInstance).thenReturn(mockPublisher); |
| 175 | + SpringWebSocketClientEventListener fullModeEventListener = buildEventListener(true); |
| 176 | + ContextRefreshedEvent event = new ContextRefreshedEvent(applicationContext); |
| 177 | + fullModeEventListener.onApplicationEvent(event); |
| 178 | + fullModeEventListener.onApplicationEvent(event); |
| 179 | + verify(mockPublisher, times(1)).start(any()); |
| 180 | + verify(mockPublisher, times(1)).publishEvent(any()); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private SpringWebSocketClientEventListener buildEventListener(final boolean full) { |
| 185 | + Properties properties = new Properties(); |
| 186 | + properties.setProperty("appName", "appName"); |
| 187 | + properties.setProperty("contextPath", "contextPath"); |
| 188 | + properties.setProperty(ShenyuClientConstants.PORT, "8080"); |
| 189 | + properties.setProperty(ShenyuClientConstants.HOST, "127.0.0.1"); |
| 190 | + properties.setProperty(ShenyuClientConstants.IP_PORT, "127.0.0.1:8080"); |
| 191 | + properties.setProperty(ShenyuClientConstants.IS_FULL, String.valueOf(full)); |
| 192 | + properties.setProperty(ShenyuClientConstants.DISCOVERY_LOCAL_MODE_KEY, Boolean.TRUE.toString()); |
| 193 | + |
| 194 | + ShenyuClientConfig clientConfig = mock(ShenyuClientConfig.class); |
| 195 | + Map<String, ClientPropertiesConfig> client = new HashMap<>(); |
| 196 | + ClientPropertiesConfig clientPropertiesConfig = new ClientPropertiesConfig(); |
| 197 | + clientPropertiesConfig.setProps(properties); |
| 198 | + client.put(RpcTypeEnum.WEB_SOCKET.getName(), clientPropertiesConfig); |
| 199 | + when(clientConfig.getClient()).thenReturn(client); |
| 200 | + return new SpringWebSocketClientEventListener(clientConfig, registerRepository); |
| 201 | + } |
| 202 | + |
181 | 203 | /** |
182 | 204 | * class for mock. |
183 | 205 | */ |
|
0 commit comments