Skip to content

Commit 9be3229

Browse files
authored
Implement tests for EurekaReactiveDiscoveryClient and EurekaDiscoveryClientTests (#4508)
* Implement probe() and reactiveProbe() and tests for Reactive client Signed-off-by: Mohamed Macow <[email protected]> * Implement test for probe() Signed-off-by: Mohamed Macow <[email protected]> * Correct year in copyright Signed-off-by: Mohamed Macow <[email protected]> * remove function override and correct tests Signed-off-by: Mohamed Macow <[email protected]> * revert override in reactive client Signed-off-by: Mohamed Macow <[email protected]> * revert format Signed-off-by: Mohamed Macow <[email protected]> --------- Signed-off-by: Mohamed Macow <[email protected]>
1 parent e570ec2 commit 9be3229

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2025-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.netflix.eureka;
18+
19+
import com.netflix.discovery.EurekaClient;
20+
import com.netflix.discovery.shared.Applications;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.ExtendWith;
23+
import org.mockito.InjectMocks;
24+
import org.mockito.Mock;
25+
import org.mockito.junit.jupiter.MockitoExtension;
26+
27+
import static org.assertj.core.api.Assertions.assertThatCode;
28+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
29+
import static org.mockito.Mockito.when;
30+
31+
32+
/**
33+
* @author Mohamed Macow
34+
*/
35+
@ExtendWith(MockitoExtension.class)
36+
class EurekaDiscoveryClientTests {
37+
38+
@Mock
39+
private EurekaClient eurekaClient;
40+
41+
@InjectMocks
42+
private EurekaDiscoveryClient client;
43+
44+
@Test
45+
void shouldCompleteProbeWhenClientHealthy() {
46+
when(eurekaClient.getApplications()).thenReturn(new Applications());
47+
48+
assertThatCode(() -> client.probe())
49+
.doesNotThrowAnyException();
50+
}
51+
52+
@Test
53+
void shouldThrowProbeWhenClientThrows() {
54+
RuntimeException eurekaException = new RuntimeException("exception");
55+
when(eurekaClient.getApplications()).thenThrow(eurekaException);
56+
57+
assertThatExceptionOfType(eurekaException.getClass())
58+
.isThrownBy(() -> client.probe())
59+
.withMessage(eurekaException.getMessage());
60+
}
61+
}

spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/reactive/EurekaReactiveDiscoveryClientTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
/**
4141
* @author Tim Ysewyn
42+
* @author Mohamed Macow
4243
*/
4344
@ExtendWith(MockitoExtension.class)
4445
class EurekaReactiveDiscoveryClientTests {
@@ -109,4 +110,24 @@ void shouldReturnFluxOfServiceInstances() {
109110
StepVerifier.create(instances).expectNextCount(1).expectComplete().verify();
110111
}
111112

113+
@Test
114+
void shouldCompleteReactiveProbeWhenClientHealthy() {
115+
when(eurekaClient.getApplications()).thenReturn(new Applications());
116+
117+
StepVerifier.create(client.reactiveProbe())
118+
.verifyComplete();
119+
}
120+
121+
@Test
122+
void shouldErrorReactiveProbeWhenClientThrows() {
123+
RuntimeException eurekaException = new RuntimeException("exception");
124+
when(eurekaClient.getApplications()).thenThrow(eurekaException);
125+
126+
StepVerifier.create(client.reactiveProbe())
127+
.verifyErrorSatisfies(ex ->
128+
assertThat(ex)
129+
.isInstanceOf(RuntimeException.class)
130+
.hasMessage(eurekaException.getMessage()));
131+
}
132+
112133
}

0 commit comments

Comments
 (0)