Skip to content

Commit c4b33cd

Browse files
committed
Code coverage for custom health checks
1 parent ab2c8ac commit c4b33cd

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.learning.contextservice;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.actuate.health.Health;
5+
import org.springframework.test.util.ReflectionTestUtils;
6+
import org.springframework.boot.actuate.health.Status;
7+
8+
import java.util.function.BooleanSupplier;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
class MyCustomHealthCheckTest {
13+
14+
@Test
15+
void testHealthUp() {
16+
MyCustomHealthCheck healthCheck = new MyCustomHealthCheck();
17+
// Simulate a healthy state
18+
ReflectionTestUtils.setField(healthCheck, "isHealthy", true);
19+
Health health = healthCheck.health();
20+
assertEquals(Status.UP, health.getStatus());
21+
assertTrue(health.getDetails().containsKey("message"));
22+
assertEquals("Service is running and scheduled checks are OK", health.getDetails().get("message"));
23+
}
24+
25+
@Test
26+
void testHealthDown() {
27+
MyCustomHealthCheck healthCheck = new MyCustomHealthCheck();
28+
// Simulate an unhealthy state
29+
ReflectionTestUtils.setField(healthCheck, "isHealthy", false);
30+
Health health = healthCheck.health();
31+
assertEquals(Status.DOWN, health.getStatus());
32+
assertTrue(health.getDetails().containsKey("error"));
33+
assertEquals("Scheduled health checks failed", health.getDetails().get("error"));
34+
}
35+
36+
@Test
37+
void testUpdateHealthStatusSetsIsHealthy() throws Exception {
38+
MyCustomHealthCheck healthCheck = new MyCustomHealthCheck();
39+
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+
healthCheck.updateHealthStatus();
48+
assertTrue((Boolean) ReflectionTestUtils.getField(healthCheck, "isHealthy"), "Health should be true");
49+
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");
57+
healthCheck.updateHealthStatus();
58+
assertFalse((Boolean) ReflectionTestUtils.getField(healthCheck, "isHealthy"), "Health should be false");
59+
}
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.
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.learning.greetingservice;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.actuate.health.Health;
5+
import org.springframework.test.util.ReflectionTestUtils;
6+
import org.springframework.boot.actuate.health.Status;
7+
8+
import java.util.function.BooleanSupplier;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
class MyCustomHealthCheckTest {
13+
14+
@Test
15+
void testHealthUp() {
16+
MyCustomHealthCheck healthCheck = new MyCustomHealthCheck();
17+
// Simulate a healthy state
18+
ReflectionTestUtils.setField(healthCheck, "isHealthy", true);
19+
Health health = healthCheck.health();
20+
assertEquals(Status.UP, health.getStatus());
21+
assertTrue(health.getDetails().containsKey("message"));
22+
assertEquals("Service is running and scheduled checks are OK", health.getDetails().get("message"));
23+
}
24+
25+
@Test
26+
void testHealthDown() {
27+
MyCustomHealthCheck healthCheck = new MyCustomHealthCheck();
28+
// Simulate an unhealthy state
29+
ReflectionTestUtils.setField(healthCheck, "isHealthy", false);
30+
Health health = healthCheck.health();
31+
assertEquals(Status.DOWN, health.getStatus());
32+
assertTrue(health.getDetails().containsKey("error"));
33+
assertEquals("Scheduled health checks failed", health.getDetails().get("error"));
34+
}
35+
36+
@Test
37+
void testUpdateHealthStatusSetsIsHealthy() throws Exception {
38+
MyCustomHealthCheck healthCheck = new MyCustomHealthCheck();
39+
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+
healthCheck.updateHealthStatus();
48+
assertTrue((Boolean) ReflectionTestUtils.getField(healthCheck, "isHealthy"), "Health should be true");
49+
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");
57+
healthCheck.updateHealthStatus();
58+
assertFalse((Boolean) ReflectionTestUtils.getField(healthCheck, "isHealthy"), "Health should be false");
59+
}
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.
66+
}

0 commit comments

Comments
 (0)