11/*
2- * Copyright 2020 the original author or authors.
2+ * Copyright 2020-2022 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1919import static org .assertj .core .api .Assertions .assertThat ;
2020import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
2121import static org .mockito .ArgumentMatchers .any ;
22- import static org .mockito .ArgumentMatchers .anyBoolean ;
2322import static org .mockito .BDDMockito .given ;
2423import static org .mockito .Mockito .mock ;
2524import static org .mockito .Mockito .times ;
3130
3231import org .junit .jupiter .api .Test ;
3332
33+ import org .springframework .beans .factory .ObjectProvider ;
3434import org .springframework .context .ApplicationContext ;
3535import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
3636import org .springframework .context .annotation .Bean ;
@@ -51,9 +51,10 @@ public class MicrometerHolderTests {
5151 void testMicrometerHolderRecordSuccessWorksGracefullyAfterDestroy () {
5252 MeterRegistry meterRegistry = new SimpleMeterRegistry ();
5353 ApplicationContext ctx = mock (ApplicationContext .class );
54+ ObjectProvider <MeterRegistry > beanProvider = mock (ObjectProvider .class );
55+ given (ctx .getBeanProvider (MeterRegistry .class )).willReturn (beanProvider );
5456 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 );
5758
5859 MicrometerHolder micrometerHolder = new MicrometerHolder (ctx , "holderName" ,
5960 "timerName" , "timerDesc" , Collections .emptyMap ());
@@ -68,21 +69,24 @@ void testMicrometerHolderRecordSuccessWorksGracefullyAfterDestroy() {
6869
6970 micrometerHolder .success (sample );
7071
71- verify (ctx , times (1 )).getBeansOfType (any (), anyBoolean (), anyBoolean ( ));
72+ verify (ctx , times (1 )).getBeanProvider (any (Class . class ));
7273 verify (sample , times (1 )).stop (any ());
7374 verifyNoMoreInteractions (ctx , sample );
7475 }
7576
7677 @ Test
7778 void multiReg () {
7879 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)" );
8083 }
8184
8285 @ Test
8386 void twoPrimaries () {
8487 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" );
8690 }
8791
8892 @ Test
0 commit comments