1717package org .springframework .integration .support .management .micrometer ;
1818
1919import static org .assertj .core .api .Assertions .assertThat ;
20- import static org .assertj .core .api .Assertions .fail ;
20+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
2121
2222import java .util .Set ;
2323
24- import org .junit .Test ;
25- import org .junit .runner .RunWith ;
24+ import org .junit .jupiter .api .Test ;
2625
2726import org .springframework .beans .factory .annotation .Autowired ;
2827import org .springframework .beans .factory .support .BeanDefinitionBuilder ;
5049import org .springframework .messaging .support .GenericMessage ;
5150import org .springframework .test .annotation .DirtiesContext ;
5251import org .springframework .test .context .TestExecutionListeners ;
53- import org .springframework .test .context .junit4 . SpringRunner ;
52+ import org .springframework .test .context .junit . jupiter . SpringJUnitConfig ;
5453import org .springframework .test .context .support .DependencyInjectionTestExecutionListener ;
5554
5655import io .micrometer .core .instrument .MeterRegistry ;
6463 * @since 5.0.2
6564 *
6665 */
67- @ RunWith ( SpringRunner . class )
66+ @ SpringJUnitConfig
6867@ DirtiesContext
6968@ TestExecutionListeners (DependencyInjectionTestExecutionListener .class )
7069public class MicrometerMetricsTests {
@@ -98,33 +97,25 @@ public class MicrometerMetricsTests {
9897 public void testSend () {
9998 GenericMessage <String > message = new GenericMessage <>("foo" );
10099 this .channel .send (message );
101- try {
102- this .channel .send (this .source .receive ()); // "bar"
103- fail ("Expected exception" );
104- }
105- catch (MessagingException e ) {
106- assertThat (e .getCause ().getMessage ()).isEqualTo ("testErrorCount" );
107- }
108- try {
109- this .channel .send (new GenericMessage <>("bar" ));
110- fail ("Expected exception" );
111- }
112- catch (MessagingException e ) {
113- assertThat (e .getCause ().getMessage ()).isEqualTo ("testErrorCount" );
114- }
100+ assertThatExceptionOfType (MessagingException .class )
101+ .isThrownBy (() -> this .channel .send (this .source .receive ())) // "bar"
102+ .withStackTraceContaining ("testErrorCount" );
103+
104+ assertThatExceptionOfType (MessagingException .class )
105+ .isThrownBy (() -> this .channel .send (new GenericMessage <>("bar" )))
106+ .withStackTraceContaining ("testErrorCount" );
107+
115108 assertThat (TestUtils .getPropertyValue (this .channel , "meters" , Set .class )).hasSize (2 );
116109 this .channel2 .send (message );
117110 this .queue .send (message );
118111 this .queue .send (message );
119112 this .queue .receive ();
120113 this .badPoll .send (message );
121- try {
122- this .badPoll .receive ();
123- fail ("Expected exception" );
124- }
125- catch (RuntimeException e ) {
126- assertThat (e .getMessage ()).isEqualTo ("badPoll" );
127- }
114+
115+ assertThatExceptionOfType (RuntimeException .class )
116+ .isThrownBy (() -> this .badPoll .receive ())
117+ .withMessage ("badPoll" );
118+
128119 nullChannel .send (message );
129120 MeterRegistry registry = this .meterRegistry ;
130121 assertThat (registry .get ("spring.integration.channels" ).gauge ().value ()).isEqualTo (6 );
@@ -147,7 +138,7 @@ public void testSend() {
147138 .timer ().count ()).isEqualTo (1 );
148139
149140 assertThat (registry .get ("spring.integration.send" )
150- .tag ("name" , "eipMethod.handler " )
141+ .tag ("name" , "eipMethod" )
151142 .tag ("result" , "success" )
152143 .timer ().count ()).isEqualTo (1 );
153144
@@ -162,7 +153,7 @@ public void testSend() {
162153 .timer ().count ()).isEqualTo (2 );
163154
164155 assertThat (registry .get ("spring.integration.send" )
165- .tag ("name" , "eipMethod.handler " )
156+ .tag ("name" , "eipMethod" )
166157 .tag ("result" , "failure" )
167158 .timer ().count ()).isEqualTo (2 );
168159
@@ -194,32 +185,26 @@ public void testSend() {
194185 .tag ("result" , "success" )
195186 .timer ();
196187 newChannel .destroy ();
197- try {
198- registry .get ("spring.integration.send" )
199- .tag ("name" , "newChannel" )
200- .tag ("result" , "success" )
201- .timer ();
202- fail ("Expected MeterNotFoundException" );
203- }
204- catch (MeterNotFoundException e ) {
205- assertThat (e ).hasMessageContaining ("A meter with name 'spring.integration.send' was found" );
206- assertThat (e ).hasMessageContaining ("No meters have a tag 'name' with value 'newChannel'" );
207- }
188+
189+ assertThatExceptionOfType (MeterNotFoundException .class )
190+ .isThrownBy (() ->
191+ registry .get ("spring.integration.send" )
192+ .tag ("name" , "newChannel" )
193+ .tag ("result" , "success" )
194+ .timer ())
195+ .withStackTraceContaining ("A meter with name 'spring.integration.send' was found" )
196+ .withStackTraceContaining ("No meters have a tag 'name' with value 'newChannel'" );
197+
208198 this .context .close ();
209- try {
210- registry .get ("spring.integration.send" ).timers ();
211- fail ("Expected MeterNotFoundException" );
212- }
213- catch (MeterNotFoundException e ) {
214- assertThat (e ).hasMessageContaining ("No meter with name 'spring.integration.send' was found" );
215- }
216- try {
217- registry .get ("spring.integration.receive" ).counters ();
218- fail ("Expected MeterNotFoundException" );
219- }
220- catch (MeterNotFoundException e ) {
221- assertThat (e ).hasMessageContaining ("No meter with name 'spring.integration.receive' was found" );
222- }
199+
200+ assertThatExceptionOfType (MeterNotFoundException .class )
201+ .isThrownBy (() -> registry .get ("spring.integration.send" ).timers ())
202+ .withStackTraceContaining ("No meter with name 'spring.integration.send' was found" );
203+
204+ assertThatExceptionOfType (MeterNotFoundException .class )
205+ .isThrownBy (() -> registry .get ("spring.integration.receive" ).counters ())
206+ .withStackTraceContaining ("No meter with name 'spring.integration.receive' was found" );
207+
223208 this .channel .destroy ();
224209 assertThat (TestUtils .getPropertyValue (this .channel , "meters" , Set .class )).hasSize (0 );
225210
0 commit comments