1717package org .springframework .integration .configuration ;
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 ;
21+ import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
2122import static org .mockito .Mockito .doAnswer ;
2223import static org .mockito .Mockito .mock ;
2324import static org .mockito .Mockito .spy ;
4344
4445import org .springframework .beans .DirectFieldAccessor ;
4546import org .springframework .beans .factory .FactoryBean ;
47+ import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
4648import org .springframework .beans .factory .annotation .Autowired ;
4749import org .springframework .beans .factory .annotation .Qualifier ;
4850import org .springframework .beans .factory .config .AbstractFactoryBean ;
5355import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
5456import org .springframework .context .annotation .Bean ;
5557import org .springframework .context .annotation .ComponentScan ;
58+ import org .springframework .context .annotation .Condition ;
59+ import org .springframework .context .annotation .ConditionContext ;
60+ import org .springframework .context .annotation .Conditional ;
5661import org .springframework .context .annotation .Configuration ;
5762import org .springframework .context .annotation .ImportResource ;
5863import org .springframework .context .expression .EnvironmentAccessor ;
5964import org .springframework .context .expression .MapAccessor ;
6065import org .springframework .core .convert .converter .Converter ;
6166import org .springframework .core .serializer .support .SerializingConverter ;
67+ import org .springframework .core .type .AnnotatedTypeMetadata ;
6268import org .springframework .expression .EvaluationContext ;
6369import org .springframework .expression .spel .support .ReflectivePropertyAccessor ;
6470import org .springframework .expression .spel .support .StandardEvaluationContext ;
@@ -432,15 +438,11 @@ public void testAnnotatedServiceActivator() throws Exception {
432438 @ Test
433439 @ DirtiesContext
434440 public void testChangePatterns () {
435- try {
436- this .configurer .setComponentNamePatterns (new String [] { "*" });
437- fail ("ExpectedException" );
438- }
439- catch (IllegalStateException e ) {
440- assertThat (e .getMessage ()).contains ("cannot be changed" );
441- }
441+ assertThatIllegalStateException ()
442+ .isThrownBy (() -> this .configurer .setComponentNamePatterns (new String []{ "*" }))
443+ .withMessageContaining ("cannot be changed" );
442444 this .configurer .stop ();
443- this .configurer .setComponentNamePatterns (new String [] { "*" });
445+ this .configurer .setComponentNamePatterns (new String []{ "*" });
444446 assertThat (TestUtils .getPropertyValue (this .configurer , "componentNamePatterns" , String [].class )[0 ])
445447 .isEqualTo ("*" );
446448 }
@@ -468,6 +470,9 @@ public void testMessagingGateway() throws InterruptedException {
468470 this .testGateway .sendAsync ("foo" );
469471 assertThat (this .asyncAnnotationProcessLatch .await (1 , TimeUnit .SECONDS )).isTrue ();
470472 assertThat (this .asyncAnnotationProcessThread .get ()).isNotSameAs (Thread .currentThread ());
473+
474+ assertThatExceptionOfType (NoSuchBeanDefinitionException .class )
475+ .isThrownBy (() -> this .context .getBean (ConditionalGateway .class ));
471476 }
472477
473478 /**
@@ -620,14 +625,10 @@ public void testBridgeAnnotations() {
620625 assertThat (testMessage ).isSameAs (receive );
621626 assertThat (this .pollableBridgeOutput .receive (10 )).isNull ();
622627
623- try {
624- this .metaBridgeInput .send (testMessage );
625- fail ("MessageDeliveryException expected" );
626- }
627- catch (Exception e ) {
628- assertThat (e ).isInstanceOf (MessageDeliveryException .class );
629- assertThat (e .getMessage ()).contains ("Dispatcher has no subscribers" );
630- }
628+
629+ assertThatExceptionOfType (MessageDeliveryException .class )
630+ .isThrownBy (() -> this .metaBridgeInput .send (testMessage ))
631+ .withMessageContaining ("Dispatcher has no subscribers" );
631632
632633 this .context .getBean ("enableIntegrationTests.ContextConfiguration.metaBridgeOutput.bridgeFrom" ,
633634 Lifecycle .class ).start ();
@@ -652,14 +653,9 @@ public void testBridgeAnnotations() {
652653 assertThat (bridgeMessage ).isSameAs (receive );
653654 assertThat (replyChannel .receive (10 )).isNull ();
654655
655- try {
656- this .myBridgeToInput .send (testMessage );
657- fail ("MessageDeliveryException expected" );
658- }
659- catch (Exception e ) {
660- assertThat (e ).isInstanceOf (MessageDeliveryException .class );
661- assertThat (e .getMessage ()).contains ("Dispatcher has no subscribers" );
662- }
656+ assertThatExceptionOfType (MessageDeliveryException .class )
657+ .isThrownBy (() -> this .myBridgeToInput .send (testMessage ))
658+ .withMessageContaining ("Dispatcher has no subscribers" );
663659
664660 this .context .getBean ("enableIntegrationTests.ContextConfiguration.myBridgeToInput.bridgeTo" ,
665661 Lifecycle .class ).start ();
@@ -762,7 +758,7 @@ public void testIntegrationEvaluationContextCustomization() {
762758 private PollableChannel myHandlerSuccessChannel ;
763759
764760 @ Test
765- public void testAdvicedServiceActivator () {
761+ public void testAdvisedServiceActivator () {
766762 Date testDate = new Date ();
767763
768764 this .myHandlerChannel .send (new GenericMessage <>(testDate ));
@@ -1411,6 +1407,14 @@ public boolean isRunning() {
14111407
14121408 }
14131409
1410+ @ Conditional (TestCondition .class )
1411+ @ MessagingGateway
1412+ public interface ConditionalGateway {
1413+
1414+ void testGateway (Object payload );
1415+
1416+ }
1417+
14141418 @ TestMessagingGateway
14151419 public interface TestGateway {
14161420
@@ -1673,4 +1677,13 @@ public static Object bar(Object o) {
16731677
16741678 }
16751679
1680+ public static class TestCondition implements Condition {
1681+
1682+ @ Override
1683+ public boolean matches (ConditionContext context , AnnotatedTypeMetadata metadata ) {
1684+ return context .getBeanFactory ().containsBean ("DoesNotExist" );
1685+ }
1686+
1687+ }
1688+
16761689}
0 commit comments