File tree Expand file tree Collapse file tree 5 files changed +168
-0
lines changed
spring-boot-modules/spring-boot-3-observation
main/java/com/baeldung/micrometer/test
test/java/com/baeldung/micrometer/test Expand file tree Collapse file tree 5 files changed +168
-0
lines changed Original file line number Diff line number Diff line change 3232 <artifactId >micrometer-tracing-bridge-brave</artifactId >
3333 <!-- artifactId>micrometer-tracing-bridge-otel</artifactId -->
3434 </dependency >
35+ <dependency >
36+ <groupId >io.micrometer</groupId >
37+ <artifactId >micrometer-test</artifactId >
38+ <scope >test</scope >
39+ </dependency >
3540 <dependency >
3641 <groupId >io.micrometer</groupId >
3742 <artifactId >micrometer-observation-test</artifactId >
Original file line number Diff line number Diff line change 1+ package com .baeldung .micrometer .test ;
2+
3+ import org .springframework .boot .SpringApplication ;
4+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
5+
6+ @ SpringBootApplication
7+ public class FooApplication {
8+
9+ public static void main (String [] args ) {
10+ SpringApplication .run (FooApplication .class , args );
11+ }
12+
13+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .micrometer .test ;
2+
3+ import java .util .concurrent .ThreadLocalRandom ;
4+
5+ import org .springframework .stereotype .Service ;
6+
7+ import io .micrometer .core .instrument .MeterRegistry ;
8+
9+ @ Service
10+ public class FooService {
11+
12+ private final MeterRegistry registry ;
13+
14+ public FooService (MeterRegistry registry ) {
15+ this .registry = registry ;
16+ }
17+
18+ public int foo () {
19+ int delayedMs = registry .timer ("foo.time" )
20+ .record (this ::doSomething );
21+
22+ registry .counter ("foo.count" )
23+ .increment ();
24+
25+ return delayedMs ;
26+ }
27+
28+ private int doSomething () {
29+ int delayMs = ThreadLocalRandom .current ()
30+ .nextInt (10 , 100 );
31+ try {
32+ Thread .sleep (delayMs );
33+ return delayMs ;
34+ } catch (InterruptedException e ) {
35+ throw new RuntimeException (e );
36+ }
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .micrometer .test ;
2+
3+ import static java .time .Duration .ofMillis ;
4+ import static org .assertj .core .api .Assertions .assertThat ;
5+
6+ import java .util .concurrent .TimeUnit ;
7+
8+ import org .junit .jupiter .api .BeforeEach ;
9+ import org .junit .jupiter .api .Test ;
10+ import org .springframework .beans .factory .annotation .Autowired ;
11+ import org .springframework .boot .test .context .SpringBootTest ;
12+
13+ import io .micrometer .core .instrument .MeterRegistry ;
14+
15+ @ SpringBootTest (classes = FooApplication .class )
16+ class MicrometerIntegrationTest {
17+
18+ @ Autowired
19+ private MeterRegistry meterRegistry ;
20+
21+ @ Autowired
22+ private FooService fooService ;
23+
24+ @ BeforeEach
25+ void reset () {
26+ meterRegistry .clear ();
27+ }
28+
29+ @ Test
30+ void whenFooIsCalled_thenCounterIsIncremented () {
31+ fooService .foo ();
32+ fooService .foo ();
33+ fooService .foo ();
34+
35+ double invocations = meterRegistry .get ("foo.count" )
36+ .counter ()
37+ .count ();
38+
39+ assertThat (invocations )
40+ .isEqualTo (3 );
41+ }
42+
43+ @ Test
44+ void whenFooIsCalled_thenTimerIsUpdated () {
45+ fooService .foo ();
46+ fooService .foo ();
47+ fooService .foo ();
48+
49+ int totalTimeMs = (int ) meterRegistry .get ("foo.time" )
50+ .timer ()
51+ .totalTime (TimeUnit .MILLISECONDS );
52+
53+ assertThat (ofMillis (totalTimeMs ))
54+ .isBetween (ofMillis (30 ), ofMillis (400 ));
55+ }
56+
57+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .micrometer .test ;
2+
3+ import static java .time .Duration .ofMillis ;
4+ import static org .assertj .core .api .Assertions .assertThat ;
5+
6+ import java .util .concurrent .TimeUnit ;
7+
8+ import org .junit .jupiter .api .Test ;
9+
10+ import io .micrometer .core .instrument .MeterRegistry ;
11+ import io .micrometer .core .instrument .simple .SimpleMeterRegistry ;
12+ import io .micrometer .core .tck .MeterRegistryAssert ;
13+
14+ class MicrometerUnitTest {
15+
16+ private MeterRegistry meterRegistry = new SimpleMeterRegistry ();
17+ private FooService fooService = new FooService (meterRegistry );
18+
19+ @ Test
20+ void whenFooIsCalled_thenCounterIsIncremented () {
21+ fooService .foo ();
22+ fooService .foo ();
23+ fooService .foo ();
24+
25+ double invocations = meterRegistry .get ("foo.count" )
26+ .counter ()
27+ .count ();
28+
29+ assertThat (invocations )
30+ .isEqualTo (3 );
31+ }
32+
33+ @ Test
34+ void whenFooIsCalled_thenTimerIsUpdated () {
35+ fooService .foo ();
36+ fooService .foo ();
37+ fooService .foo ();
38+
39+ int totalTimeMs = (int ) meterRegistry .get ("foo.time" )
40+ .timer ()
41+ .totalTime (TimeUnit .MILLISECONDS );
42+
43+ assertThat (ofMillis (totalTimeMs ))
44+ .isBetween (ofMillis (30 ), ofMillis (400 ));
45+ }
46+
47+ @ Test
48+ void whenFooIsCalled_thenTimerIsRegistered () {
49+ fooService .foo ();
50+
51+ MeterRegistryAssert .assertThat (meterRegistry )
52+ .hasTimerWithName ("foo.time" );
53+ }
54+
55+ }
You can’t perform that action at this time.
0 commit comments