Skip to content

Commit edb73c1

Browse files
committed
changes for unit tests
1 parent c4b33cd commit edb73c1

File tree

5 files changed

+30
-51
lines changed

5 files changed

+30
-51
lines changed

Diff for: microservices-self-registration/contextservice/src/main/java/com/learning/contextservice/MyCustomHealthCheck.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.boot.actuate.health.HealthIndicator;
77
import org.springframework.scheduling.annotation.Scheduled;
88
import org.springframework.stereotype.Component;
9+
import java.time.Clock;
910

1011
@Component("myCustomHealthCheck")
1112
public class MyCustomHealthCheck implements HealthIndicator {
@@ -22,7 +23,7 @@ public void updateHealthStatus() {
2223
log.info("Update health status : {}", isHealthy);
2324
}
2425

25-
private boolean performHealthCheck() {
26+
boolean performHealthCheck() {
2627
boolean current = System.currentTimeMillis() % 10000 < 5000; // Simulate fluctuating health
2728
log.debug("Performing health check, current status: {}", current);
2829
return current; // Simulate fluctuating health
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
package com.learning.contextservice;
22

33
import org.junit.jupiter.api.Test;
4+
import org.mockito.Mockito;
45
import org.springframework.boot.actuate.health.Health;
56
import org.springframework.test.util.ReflectionTestUtils;
67
import org.springframework.boot.actuate.health.Status;
78

9+
import java.time.Clock;
10+
import java.time.Instant;
11+
import java.time.ZoneId;
812
import java.util.function.BooleanSupplier;
913

1014
import static org.junit.jupiter.api.Assertions.*;
15+
import static org.mockito.Mockito.doReturn;
1116

1217
class MyCustomHealthCheckTest {
1318

19+
MyCustomHealthCheck healthCheck = new MyCustomHealthCheck();
20+
1421
@Test
1522
void testHealthUp() {
1623
MyCustomHealthCheck healthCheck = new MyCustomHealthCheck();
@@ -34,33 +41,17 @@ void testHealthDown() {
3441
}
3542

3643
@Test
37-
void testUpdateHealthStatusSetsIsHealthy() throws Exception {
38-
MyCustomHealthCheck healthCheck = new MyCustomHealthCheck();
44+
void testUpdateHealthStatusSetsIsHealthyWithMocking() throws Exception {
45+
MyCustomHealthCheck healthCheck = Mockito.spy(new MyCustomHealthCheck());
3946

40-
// Force performHealthCheck to return true (by time simulation - might be flaky)
41-
long currentTimeForTrue = System.currentTimeMillis();
42-
while (currentTimeForTrue % 10000 >= 5000) {
43-
Thread.sleep(10); // Wait until time is in the "true" range
44-
currentTimeForTrue = System.currentTimeMillis();
45-
}
46-
ReflectionTestUtils.invokeMethod(healthCheck, "performHealthCheck");
47+
// Force performHealthCheck to return true
48+
doReturn(true).when(healthCheck).performHealthCheck();
4749
healthCheck.updateHealthStatus();
48-
assertTrue((Boolean) ReflectionTestUtils.getField(healthCheck, "isHealthy"), "Health should be true");
50+
assertTrue((Boolean)ReflectionTestUtils.getField(healthCheck, "isHealthy"), "Health should be true");
4951

50-
// Force performHealthCheck to return false (by time simulation - might be flaky)
51-
long currentTimeForFalse = System.currentTimeMillis();
52-
while (currentTimeForFalse % 10000 < 5000) {
53-
Thread.sleep(10); // Wait until time is in the "false" range
54-
currentTimeForFalse = System.currentTimeMillis();
55-
}
56-
ReflectionTestUtils.invokeMethod(healthCheck, "performHealthCheck");
52+
// Force performHealthCheck to return false
53+
doReturn(false).when(healthCheck).performHealthCheck();
5754
healthCheck.updateHealthStatus();
58-
assertFalse((Boolean) ReflectionTestUtils.getField(healthCheck, "isHealthy"), "Health should be false");
55+
assertFalse((Boolean)ReflectionTestUtils.getField(healthCheck, "isHealthy"), "Health should be false");
5956
}
60-
61-
// Note: Directly testing performHealthCheck (the private method) based on time is inherently
62-
// difficult and can lead to flaky tests. The testUpdateHealthStatus attempts to indirectly
63-
// verify its behavior. For more robust testing of performHealthCheck in isolation,
64-
// you might consider refactoring the class to make the time dependency injectable
65-
// or making the method protected for testing purposes.
6657
}

Diff for: microservices-self-registration/eurekaserver/pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
<groupId>org.springframework.cloud</groupId>
2222
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
2323
</dependency>
24-
2524
<dependency>
2625
<groupId>org.springframework.boot</groupId>
2726
<artifactId>spring-boot-starter-test</artifactId>

Diff for: microservices-self-registration/greetingservice/src/main/java/com/learning/greetingservice/MyCustomHealthCheck.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void updateHealthStatus() {
2222
log.info("Update health status : {}", isHealthy);
2323
}
2424

25-
private boolean performHealthCheck() {
25+
boolean performHealthCheck() {
2626
boolean current = System.currentTimeMillis() % 10000 < 5000; // Simulate fluctuating health
2727
log.debug("Performing health check, current status: {}", current);
2828
return current; // Simulate fluctuating health
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.learning.greetingservice;
22

33
import org.junit.jupiter.api.Test;
4+
import org.mockito.Mockito;
45
import org.springframework.boot.actuate.health.Health;
56
import org.springframework.test.util.ReflectionTestUtils;
67
import org.springframework.boot.actuate.health.Status;
78

89
import java.util.function.BooleanSupplier;
910

1011
import static org.junit.jupiter.api.Assertions.*;
12+
import static org.mockito.Mockito.doReturn;
1113

1214
class MyCustomHealthCheckTest {
1315

@@ -34,33 +36,19 @@ void testHealthDown() {
3436
}
3537

3638
@Test
37-
void testUpdateHealthStatusSetsIsHealthy() throws Exception {
38-
MyCustomHealthCheck healthCheck = new MyCustomHealthCheck();
39+
void testUpdateHealthStatusSetsIsHealthyWithMocking() throws Exception {
40+
MyCustomHealthCheck healthCheck = Mockito.spy(new MyCustomHealthCheck());
3941

40-
// Force performHealthCheck to return true (by time simulation - might be flaky)
41-
long currentTimeForTrue = System.currentTimeMillis();
42-
while (currentTimeForTrue % 10000 >= 5000) {
43-
Thread.sleep(10); // Wait until time is in the "true" range
44-
currentTimeForTrue = System.currentTimeMillis();
45-
}
46-
ReflectionTestUtils.invokeMethod(healthCheck, "performHealthCheck");
42+
// Force performHealthCheck to return true
43+
doReturn(true).when(healthCheck).performHealthCheck();
4744
healthCheck.updateHealthStatus();
48-
assertTrue((Boolean) ReflectionTestUtils.getField(healthCheck, "isHealthy"), "Health should be true");
45+
assertTrue((Boolean) ReflectionTestUtils.getField(healthCheck, "isHealthy"),
46+
"Health should be true");
4947

50-
// Force performHealthCheck to return false (by time simulation - might be flaky)
51-
long currentTimeForFalse = System.currentTimeMillis();
52-
while (currentTimeForFalse % 10000 < 5000) {
53-
Thread.sleep(10); // Wait until time is in the "false" range
54-
currentTimeForFalse = System.currentTimeMillis();
55-
}
56-
ReflectionTestUtils.invokeMethod(healthCheck, "performHealthCheck");
48+
// Force performHealthCheck to return false
49+
doReturn(false).when(healthCheck).performHealthCheck();
5750
healthCheck.updateHealthStatus();
58-
assertFalse((Boolean) ReflectionTestUtils.getField(healthCheck, "isHealthy"), "Health should be false");
51+
assertFalse((Boolean) ReflectionTestUtils.getField(healthCheck, "isHealthy"),
52+
"Health should be false");
5953
}
60-
61-
// Note: Directly testing performHealthCheck (the private method) based on time is inherently
62-
// difficult and can lead to flaky tests. The testUpdateHealthStatus attempts to indirectly
63-
// verify its behavior. For more robust testing of performHealthCheck in isolation,
64-
// you might consider refactoring the class to make the time dependency injectable
65-
// or making the method protected for testing purposes.
6654
}

0 commit comments

Comments
 (0)