19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
20
import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
21
21
import static org .mockito .ArgumentMatchers .any ;
22
- import static org .mockito .ArgumentMatchers .anyBoolean ;
23
22
import static org .mockito .BDDMockito .given ;
24
23
import static org .mockito .Mockito .mock ;
25
24
import static org .mockito .Mockito .times ;
31
30
32
31
import org .junit .jupiter .api .Test ;
33
32
33
+ import org .springframework .beans .factory .ObjectProvider ;
34
34
import org .springframework .context .ApplicationContext ;
35
35
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
36
36
import org .springframework .context .annotation .Bean ;
@@ -51,9 +51,10 @@ public class MicrometerHolderTests {
51
51
void testMicrometerHolderRecordSuccessWorksGracefullyAfterDestroy () {
52
52
MeterRegistry meterRegistry = new SimpleMeterRegistry ();
53
53
ApplicationContext ctx = mock (ApplicationContext .class );
54
+ ObjectProvider <MeterRegistry > beanProvider = mock (ObjectProvider .class );
55
+ given (ctx .getBeanProvider (MeterRegistry .class )).willReturn (beanProvider );
54
56
Timer .Sample sample = mock (Timer .Sample .class );
55
- given (ctx .getBeansOfType (any (), anyBoolean (), anyBoolean ()))
56
- .willReturn (Collections .singletonMap ("registry" , meterRegistry ));
57
+ given (beanProvider .getIfUnique ()).willReturn (meterRegistry );
57
58
58
59
MicrometerHolder micrometerHolder = new MicrometerHolder (ctx , "holderName" ,
59
60
"timerName" , "timerDesc" , Collections .emptyMap ());
@@ -68,21 +69,24 @@ void testMicrometerHolderRecordSuccessWorksGracefullyAfterDestroy() {
68
69
69
70
micrometerHolder .success (sample );
70
71
71
- verify (ctx , times (1 )).getBeansOfType (any (), anyBoolean (), anyBoolean ( ));
72
+ verify (ctx , times (1 )).getBeanProvider (any (Class . class ));
72
73
verify (sample , times (1 )).stop (any (Timer .class ));
73
74
verifyNoMoreInteractions (ctx , sample );
74
75
}
75
76
76
77
@ Test
77
78
void multiReg () {
78
79
assertThatIllegalStateException ().isThrownBy (() -> new MicrometerHolder (
79
- new AnnotationConfigApplicationContext (Config1 .class ), "" , "" , "" , Collections .emptyMap ()));
80
+ new AnnotationConfigApplicationContext (Config1 .class ), "" , "" , "" , Collections .emptyMap ()))
81
+ .withMessage ("No micrometer registry present (or more than one and "
82
+ + "there is not exactly one marked with @Primary)" );
80
83
}
81
84
82
85
@ Test
83
86
void twoPrimaries () {
84
87
assertThatIllegalStateException ().isThrownBy (() -> new MicrometerHolder (
85
- new AnnotationConfigApplicationContext (Config2 .class ), "" , "" , "" , Collections .emptyMap ()));
88
+ new AnnotationConfigApplicationContext (Config2 .class ), "" , "" , "" , Collections .emptyMap ()))
89
+ .withMessageContaining ("more than one 'primary' bean" );
86
90
}
87
91
88
92
@ Test
0 commit comments