1
1
package com .learning .contextservice ;
2
2
3
3
import org .junit .jupiter .api .Test ;
4
+ import org .mockito .Mockito ;
4
5
import org .springframework .boot .actuate .health .Health ;
5
6
import org .springframework .test .util .ReflectionTestUtils ;
6
7
import org .springframework .boot .actuate .health .Status ;
7
8
9
+ import java .time .Clock ;
10
+ import java .time .Instant ;
11
+ import java .time .ZoneId ;
8
12
import java .util .function .BooleanSupplier ;
9
13
10
14
import static org .junit .jupiter .api .Assertions .*;
15
+ import static org .mockito .Mockito .doReturn ;
11
16
12
17
class MyCustomHealthCheckTest {
13
18
19
+ MyCustomHealthCheck healthCheck = new MyCustomHealthCheck ();
20
+
14
21
@ Test
15
22
void testHealthUp () {
16
23
MyCustomHealthCheck healthCheck = new MyCustomHealthCheck ();
@@ -34,33 +41,17 @@ void testHealthDown() {
34
41
}
35
42
36
43
@ Test
37
- void testUpdateHealthStatusSetsIsHealthy () throws Exception {
38
- MyCustomHealthCheck healthCheck = new MyCustomHealthCheck ();
44
+ void testUpdateHealthStatusSetsIsHealthyWithMocking () throws Exception {
45
+ MyCustomHealthCheck healthCheck = Mockito . spy ( new MyCustomHealthCheck () );
39
46
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 ();
47
49
healthCheck .updateHealthStatus ();
48
- assertTrue ((Boolean ) ReflectionTestUtils .getField (healthCheck , "isHealthy" ), "Health should be true" );
50
+ assertTrue ((Boolean )ReflectionTestUtils .getField (healthCheck , "isHealthy" ), "Health should be true" );
49
51
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 ();
57
54
healthCheck .updateHealthStatus ();
58
- assertFalse ((Boolean ) ReflectionTestUtils .getField (healthCheck , "isHealthy" ), "Health should be false" );
55
+ assertFalse ((Boolean )ReflectionTestUtils .getField (healthCheck , "isHealthy" ), "Health should be false" );
59
56
}
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
57
}
0 commit comments