diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/log4j2/AmqpAppenderTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/log4j2/AmqpAppenderTests.java index 5a0cde468f..f183222a48 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/log4j2/AmqpAppenderTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/log4j2/AmqpAppenderTests.java @@ -64,6 +64,7 @@ import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.DefaultSaslConfig; import com.rabbitmq.client.JDKSaslConfig; +import com.rabbitmq.client.SaslConfig; import com.rabbitmq.client.impl.CRDemoMechanism; /** @@ -196,26 +197,34 @@ public void testSaslConfig() { Logger logger = LogManager.getLogger("sasl"); AmqpAppender appender = (AmqpAppender) TestUtils.getPropertyValue(logger, "context.configuration.appenders", Map.class).get("sasl1"); - assertThat(TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory", - ConnectionFactory.class).getSaslConfig()) - .isInstanceOf(DefaultSaslConfig.class) - .hasFieldOrPropertyWithValue("mechanism", "PLAIN"); + SaslConfig saslConfig = + TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory", + ConnectionFactory.class) + .getSaslConfig(); + assertThat(saslConfig, instanceOf(DefaultSaslConfig.class)); + assertEquals("PLAIN", TestUtils.getPropertyValue(saslConfig, "mechanism")); appender = (AmqpAppender) TestUtils.getPropertyValue(logger, "context.configuration.appenders", Map.class).get("sasl2"); - assertThat(TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory", - ConnectionFactory.class).getSaslConfig()) - .isInstanceOf(DefaultSaslConfig.class) - .hasFieldOrPropertyWithValue("mechanism", "EXTERNAL"); + saslConfig = + TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory", + ConnectionFactory.class) + .getSaslConfig(); + assertThat(saslConfig, instanceOf(DefaultSaslConfig.class)); + assertEquals("EXTERNAL", TestUtils.getPropertyValue(saslConfig, "mechanism")); appender = (AmqpAppender) TestUtils.getPropertyValue(logger, "context.configuration.appenders", Map.class).get("sasl3"); - assertThat(TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory", - ConnectionFactory.class).getSaslConfig()) - .isInstanceOf(JDKSaslConfig.class); + saslConfig = + TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory", + ConnectionFactory.class) + .getSaslConfig(); + assertThat(saslConfig, instanceOf(JDKSaslConfig.class)); appender = (AmqpAppender) TestUtils.getPropertyValue(logger, "context.configuration.appenders", Map.class).get("sasl4"); - assertThat(TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory", - ConnectionFactory.class).getSaslConfig()) - .isInstanceOf(CRDemoMechanism.CRDemoSaslConfig.class); + saslConfig = + TestUtils.getPropertyValue(appender, "manager.connectionFactory.rabbitConnectionFactory", + ConnectionFactory.class) + .getSaslConfig(); + assertThat(saslConfig, instanceOf(CRDemoMechanism.CRDemoSaslConfig.class)); } @Test diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/logback/AmqpAppenderTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/logback/AmqpAppenderTests.java index 3323697582..049e791f47 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/logback/AmqpAppenderTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/logback/AmqpAppenderTests.java @@ -16,8 +16,11 @@ package org.springframework.amqp.rabbit.logback; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyString; @@ -35,6 +38,7 @@ import org.springframework.amqp.UncategorizedAmqpException; import org.springframework.amqp.rabbit.connection.RabbitConnectionFactoryBean; +import org.springframework.amqp.utils.test.TestUtils; import org.springframework.core.io.ClassPathResource; import org.springframework.test.util.ReflectionTestUtils; @@ -47,6 +51,7 @@ * * @author Stephen Oakey * @author Artem Bilan + * @author Gary Russell * * @since 2.0 */ @@ -214,30 +219,34 @@ public void testSasl() { verify(bean).setUseSSL(eq(true)); ArgumentCaptor captor = ArgumentCaptor.forClass(SaslConfig.class); verify(bean).setSaslConfig(captor.capture()); - assertThat(captor.getValue()) - .isInstanceOf(DefaultSaslConfig.class) - .hasFieldOrPropertyWithValue("mechanism", "PLAIN"); + SaslConfig saslConfig = captor.getValue(); + assertThat(saslConfig, instanceOf(DefaultSaslConfig.class)); + assertEquals("PLAIN", TestUtils.getPropertyValue(saslConfig, "mechanism")); appender.setSaslConfig("DefaultSaslConfig.EXTERNAL"); appender.configureRabbitConnectionFactory(bean); verify(bean, times(2)).setSaslConfig(captor.capture()); - assertThat(captor.getValue()) - .isInstanceOf(DefaultSaslConfig.class) - .hasFieldOrPropertyWithValue("mechanism", "EXTERNAL"); + saslConfig = captor.getValue(); + assertThat(saslConfig, instanceOf(DefaultSaslConfig.class)); + assertEquals("EXTERNAL", TestUtils.getPropertyValue(saslConfig, "mechanism")); appender.setSaslConfig("JDKSaslConfig"); appender.configureRabbitConnectionFactory(bean); verify(bean, times(3)).setSaslConfig(captor.capture()); - assertThat(captor.getValue()) - .isInstanceOf(JDKSaslConfig.class); + assertThat(captor.getValue(), instanceOf(JDKSaslConfig.class)); appender.setSaslConfig("CRDemoSaslConfig"); appender.configureRabbitConnectionFactory(bean); verify(bean, times(4)).setSaslConfig(captor.capture()); - assertThat(captor.getValue()) - .isInstanceOf(CRDemoMechanism.CRDemoSaslConfig.class); + assertThat(captor.getValue(), instanceOf(CRDemoMechanism.CRDemoSaslConfig.class)); appender.setSaslConfig("junk"); - assertThatThrownBy(() -> appender.configureRabbitConnectionFactory(bean)) - .isInstanceOf(UncategorizedAmqpException.class) - .hasCauseInstanceOf(IllegalStateException.class) - .withFailMessage("Unrecognized SaslConfig: junk"); + + + try { + appender.configureRabbitConnectionFactory(bean); + } + catch (Exception e) { + assertThat(e, instanceOf(UncategorizedAmqpException.class)); + assertThat(e.getCause(), instanceOf(IllegalStateException.class)); + assertThat(e.getMessage(), containsString("Unrecognized SaslConfig: junk")); + } }