Migrate exception checking tests to use AssertJ
Migrate tests that use `@Test(expectedException=...)` or `try...fail...catch` to use AssertJ's `assertThatException` instead.
This commit is contained in:
@@ -38,11 +38,11 @@ import org.springframework.transaction.support.TransactionCallbackWithoutResult;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
@@ -167,13 +167,8 @@ public class JmsTransactionManagerTests {
|
||||
status.setRollbackOnly();
|
||||
}
|
||||
});
|
||||
try {
|
||||
tm.commit(ts);
|
||||
fail("Should have thrown UnexpectedRollbackException");
|
||||
}
|
||||
catch (UnexpectedRollbackException ex) {
|
||||
// expected
|
||||
}
|
||||
assertThatExceptionOfType(UnexpectedRollbackException.class).isThrownBy(() ->
|
||||
tm.commit(ts));
|
||||
|
||||
verify(session).rollback();
|
||||
verify(session).close();
|
||||
|
||||
@@ -60,10 +60,10 @@ import org.springframework.jndi.JndiTemplate;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.willThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -800,15 +800,9 @@ public class JmsTemplateTests {
|
||||
|
||||
willThrow(original).given(messageProducer).send(textMessage);
|
||||
|
||||
try {
|
||||
template.convertAndSend(this.queue, s);
|
||||
fail("Should have thrown JmsException");
|
||||
}
|
||||
catch (JmsException wrappedEx) {
|
||||
// expected
|
||||
assertEquals(thrownExceptionClass, wrappedEx.getClass());
|
||||
assertEquals(original, wrappedEx.getCause());
|
||||
}
|
||||
assertThatExceptionOfType(thrownExceptionClass).isThrownBy(() ->
|
||||
template.convertAndSend(this.queue, s))
|
||||
.withCause(original);
|
||||
|
||||
verify(messageProducer).close();
|
||||
verify(this.session).close();
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.jms.StubQueue;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ErrorHandler;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
@@ -67,9 +68,10 @@ public class SimpleMessageListenerContainerTests {
|
||||
assertNull(this.container.getMessageListener());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void testSettingMessageListenerToAnUnsupportedType() {
|
||||
this.container.setMessageListener("Bingo");
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
this.container.setMessageListener("Bingo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -80,16 +82,20 @@ public class SimpleMessageListenerContainerTests {
|
||||
this.container.isPubSubNoLocal());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void testSettingConcurrentConsumersToZeroIsNotAllowed() {
|
||||
this.container.setConcurrentConsumers(0);
|
||||
this.container.afterPropertiesSet();
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> {
|
||||
this.container.setConcurrentConsumers(0);
|
||||
this.container.afterPropertiesSet();
|
||||
});
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void testSettingConcurrentConsumersToANegativeValueIsNotAllowed() {
|
||||
this.container.setConcurrentConsumers(-198);
|
||||
this.container.afterPropertiesSet();
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> {
|
||||
this.container.setConcurrentConsumers(-198);
|
||||
this.container.afterPropertiesSet();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -37,12 +37,12 @@ import org.mockito.stubbing.Answer;
|
||||
import org.springframework.jms.support.converter.MessageConversionException;
|
||||
import org.springframework.jms.support.converter.SimpleMessageConverter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.willThrow;
|
||||
@@ -312,13 +312,9 @@ public class MessageListenerAdapterTests {
|
||||
return message;
|
||||
}
|
||||
};
|
||||
try {
|
||||
adapter.onMessage(sentTextMessage, session);
|
||||
fail("expected CouldNotSendReplyException with InvalidDestinationException");
|
||||
}
|
||||
catch (ReplyFailureException ex) {
|
||||
assertEquals(InvalidDestinationException.class, ex.getCause().getClass());
|
||||
}
|
||||
assertThatExceptionOfType(ReplyFailureException.class).isThrownBy(() ->
|
||||
adapter.onMessage(sentTextMessage, session))
|
||||
.withCauseExactlyInstanceOf(InvalidDestinationException.class);
|
||||
|
||||
verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
|
||||
verify(delegate).handleMessage(sentTextMessage);
|
||||
@@ -351,13 +347,9 @@ public class MessageListenerAdapterTests {
|
||||
return message;
|
||||
}
|
||||
};
|
||||
try {
|
||||
adapter.onMessage(sentTextMessage, session);
|
||||
fail("expected CouldNotSendReplyException with JMSException");
|
||||
}
|
||||
catch (ReplyFailureException ex) {
|
||||
assertEquals(JMSException.class, ex.getCause().getClass());
|
||||
}
|
||||
assertThatExceptionOfType(ReplyFailureException.class).isThrownBy(() ->
|
||||
adapter.onMessage(sentTextMessage, session))
|
||||
.withCauseExactlyInstanceOf(JMSException.class);
|
||||
|
||||
verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
|
||||
verify(messageProducer).close();
|
||||
@@ -378,11 +370,8 @@ public class MessageListenerAdapterTests {
|
||||
return message;
|
||||
}
|
||||
};
|
||||
try {
|
||||
adapter.onMessage(message, session);
|
||||
fail("expected ListenerExecutionFailedException");
|
||||
}
|
||||
catch (ListenerExecutionFailedException ex) { /* expected */ }
|
||||
assertThatExceptionOfType(ListenerExecutionFailedException.class).isThrownBy(() ->
|
||||
adapter.onMessage(message, session));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -399,13 +388,9 @@ public class MessageListenerAdapterTests {
|
||||
}
|
||||
};
|
||||
adapter.setMessageConverter(null);
|
||||
try {
|
||||
adapter.onMessage(sentTextMessage, session);
|
||||
fail("expected CouldNotSendReplyException with MessageConversionException");
|
||||
}
|
||||
catch (ReplyFailureException ex) {
|
||||
assertEquals(MessageConversionException.class, ex.getCause().getClass());
|
||||
}
|
||||
assertThatExceptionOfType(ReplyFailureException.class).isThrownBy(() ->
|
||||
adapter.onMessage(sentTextMessage, session))
|
||||
.withCauseExactlyInstanceOf(MessageConversionException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -45,10 +45,11 @@ import org.springframework.messaging.handler.annotation.support.DefaultMessageHa
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -100,18 +101,10 @@ public class MessagingMessageListenerAdapterTests {
|
||||
javax.jms.Message message = new StubTextMessage("foo");
|
||||
Session session = mock(Session.class);
|
||||
MessagingMessageListenerAdapter listener = getSimpleInstance("fail", String.class);
|
||||
|
||||
try {
|
||||
listener.onMessage(message, session);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (JMSException ex) {
|
||||
fail("Should not have thrown a JMS exception");
|
||||
}
|
||||
catch (ListenerExecutionFailedException ex) {
|
||||
assertEquals(IllegalArgumentException.class, ex.getCause().getClass());
|
||||
assertEquals("Expected test exception", ex.getCause().getMessage());
|
||||
}
|
||||
assertThatExceptionOfType(ListenerExecutionFailedException.class).isThrownBy(() ->
|
||||
listener.onMessage(message, session))
|
||||
.withCauseExactlyInstanceOf(IllegalArgumentException.class)
|
||||
.satisfies(ex -> assertThat(ex.getCause().getMessage()).isEqualTo("Expected test exception"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -120,16 +113,9 @@ public class MessagingMessageListenerAdapterTests {
|
||||
Session session = mock(Session.class);
|
||||
MessagingMessageListenerAdapter listener = getSimpleInstance("wrongParam", Integer.class);
|
||||
|
||||
try {
|
||||
listener.onMessage(message, session);
|
||||
fail("Should have thrown an exception");
|
||||
}
|
||||
catch (JMSException ex) {
|
||||
fail("Should not have thrown a JMS exception");
|
||||
}
|
||||
catch (ListenerExecutionFailedException ex) {
|
||||
assertEquals(MessageConversionException.class, ex.getCause().getClass());
|
||||
}
|
||||
assertThatExceptionOfType(ListenerExecutionFailedException.class).isThrownBy(() ->
|
||||
listener.onMessage(message, session))
|
||||
.withCauseExactlyInstanceOf(MessageConversionException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -20,6 +20,7 @@ import javax.jms.Session;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
@@ -31,10 +32,11 @@ import static org.junit.Assert.assertFalse;
|
||||
*/
|
||||
public class JmsAccessorTests {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void testChokesIfConnectionFactoryIsNotSupplied() throws Exception {
|
||||
JmsAccessor accessor = new StubJmsAccessor();
|
||||
accessor.afterPropertiesSet();
|
||||
assertThatIllegalArgumentException().isThrownBy(
|
||||
accessor::afterPropertiesSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -56,9 +58,10 @@ public class JmsAccessorTests {
|
||||
accessor.getSessionAcknowledgeMode());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void testSetAcknowledgeModeNameChokesIfBadAckModeIsSupplied() throws Exception {
|
||||
new StubJmsAccessor().setSessionAcknowledgeModeName("Tally ho chaps!");
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
new StubJmsAccessor().setSessionAcknowledgeModeName("Tally ho chaps!"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -35,9 +35,9 @@ import org.mockito.stubbing.Answer;
|
||||
import org.springframework.jms.support.converter.MessageConversionException;
|
||||
import org.springframework.jms.support.converter.SimpleMessageConverter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -129,14 +129,16 @@ public class SimpleMessageConverterTests {
|
||||
assertEquals(content, converter.fromMessage(msg));
|
||||
}
|
||||
|
||||
@Test(expected = MessageConversionException.class)
|
||||
@Test
|
||||
public void testToMessageThrowsExceptionIfGivenNullObjectToConvert() throws Exception {
|
||||
new SimpleMessageConverter().toMessage(null, null);
|
||||
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
|
||||
new SimpleMessageConverter().toMessage(null, null));
|
||||
}
|
||||
|
||||
@Test(expected = MessageConversionException.class)
|
||||
@Test
|
||||
public void testToMessageThrowsExceptionIfGivenIncompatibleObjectToConvert() throws Exception {
|
||||
new SimpleMessageConverter().toMessage(new Object(), null);
|
||||
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
|
||||
new SimpleMessageConverter().toMessage(new Object(), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -168,11 +170,8 @@ public class SimpleMessageConverterTests {
|
||||
content.put(1, "value1");
|
||||
|
||||
SimpleMessageConverter converter = new SimpleMessageConverter();
|
||||
try {
|
||||
converter.toMessage(content, session);
|
||||
fail("expected MessageConversionException");
|
||||
}
|
||||
catch (MessageConversionException ex) { /* expected */ }
|
||||
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
|
||||
converter.toMessage(content, session));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -185,11 +184,8 @@ public class SimpleMessageConverterTests {
|
||||
content.put(null, "value1");
|
||||
|
||||
SimpleMessageConverter converter = new SimpleMessageConverter();
|
||||
try {
|
||||
converter.toMessage(content, session);
|
||||
fail("expected MessageConversionException");
|
||||
}
|
||||
catch (MessageConversionException ex) { /* expected */ }
|
||||
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
|
||||
converter.toMessage(content, session));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,8 +20,8 @@ import javax.jms.ConnectionFactory;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
@@ -34,17 +34,10 @@ public class JmsDestinationAccessorTests {
|
||||
public void testChokesIfDestinationResolverIsetToNullExplicitly() throws Exception {
|
||||
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
|
||||
|
||||
try {
|
||||
JmsDestinationAccessor accessor = new StubJmsDestinationAccessor();
|
||||
accessor.setConnectionFactory(connectionFactory);
|
||||
accessor.setDestinationResolver(null);
|
||||
accessor.afterPropertiesSet();
|
||||
fail("expected IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
JmsDestinationAccessor accessor = new StubJmsDestinationAccessor();
|
||||
accessor.setConnectionFactory(connectionFactory);
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
accessor.setDestinationResolver(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -24,10 +24,11 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.jms.StubTopic;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
@@ -107,13 +108,8 @@ public class JndiDestinationResolverTests {
|
||||
};
|
||||
resolver.setDynamicDestinationResolver(dynamicResolver);
|
||||
|
||||
try {
|
||||
resolver.resolveDestinationName(session, DESTINATION_NAME, true);
|
||||
fail("expected DestinationResolutionException");
|
||||
}
|
||||
catch (DestinationResolutionException ex) {
|
||||
// expected
|
||||
}
|
||||
assertThatExceptionOfType(DestinationResolutionException.class).isThrownBy(() ->
|
||||
resolver.resolveDestinationName(session, DESTINATION_NAME, true));
|
||||
}
|
||||
|
||||
|
||||
@@ -123,9 +119,7 @@ public class JndiDestinationResolverTests {
|
||||
|
||||
@Override
|
||||
protected <T> T lookup(String jndiName, Class<T> requiredType) throws NamingException {
|
||||
if (called) {
|
||||
fail("Must not be delegating to lookup(..), must be resolving from cache.");
|
||||
}
|
||||
assertThat(called).as("delegating to lookup(..) not cache").isFalse();
|
||||
assertEquals(DESTINATION_NAME, jndiName);
|
||||
called = true;
|
||||
return requiredType.cast(DESTINATION);
|
||||
|
||||
Reference in New Issue
Block a user