Migrate away from ExpectedException (#22922)

* Add limited checkstyles to test code

Add a limited set of checkstyle rules to the test codebase to improve
code consistency.

* Fix checksyle violations in test code

* Organize imports to fix checkstyle for test code

* Migrate to assertThatExceptionOfType

Migrate aware from ExpectedException rules to AssertJ exception
assertions. Also include a checkstyle rules to ensure that the
the ExpectedException is not accidentally used in the future.

See gh-22894
This commit is contained in:
Phil Webb
2019-05-08 07:25:52 -07:00
committed by Sam Brannen
parent 7e6e3d7027
commit d7320de871
671 changed files with 3861 additions and 4601 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,9 +18,7 @@ package org.springframework.messaging.converter;
import java.util.Locale;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.convert.ConversionException;
import org.springframework.core.convert.ConversionService;
@@ -28,7 +26,7 @@ import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import static org.hamcrest.Matchers.*;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
/**
@@ -37,9 +35,6 @@ import static org.junit.Assert.*;
*/
public class GenericMessageConverterTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
private final ConversionService conversionService = new DefaultConversionService();
private final GenericMessageConverter converter = new GenericMessageConverter(conversionService);
@@ -58,8 +53,8 @@ public class GenericMessageConverterTests {
@Test
public void fromMessageWithFailedConversion() {
Message<String> content = MessageBuilder.withPayload("test not a number").build();
thrown.expect(MessageConversionException.class);
thrown.expectCause(isA(ConversionException.class));
converter.fromMessage(content, Integer.class);
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
converter.fromMessage(content, Integer.class))
.withCauseInstanceOf(ConversionException.class);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,15 +16,8 @@
package org.springframework.messaging.converter;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;
import static org.xmlunit.diff.ComparisonType.*;
import static org.xmlunit.diff.DifferenceEvaluators.*;
import static org.xmlunit.matchers.CompareMatcher.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.xml.bind.annotation.XmlRootElement;
import org.junit.Before;
@@ -35,6 +28,12 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;
import static org.xmlunit.diff.ComparisonType.*;
import static org.xmlunit.diff.DifferenceEvaluators.*;
import static org.xmlunit.matchers.CompareMatcher.*;
/**
* @author Arjen Poutsma
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ import java.util.concurrent.atomic.AtomicReference;
import org.junit.Before;
import org.junit.Test;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageDeliveryException;
@@ -38,6 +39,7 @@ import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
/**
@@ -130,9 +132,7 @@ public class GenericMessagingTemplateTests {
SubscribableChannel channel = mock(SubscribableChannel.class);
MessageHandler handler = createLateReplier(latch, failure);
doAnswer(invocation -> {
this.executor.execute(() -> {
handler.handleMessage(invocation.getArgument(0));
});
this.executor.execute(() -> handler.handleMessage(invocation.getArgument(0)));
return true;
}).when(channel).send(any(Message.class), anyLong());
@@ -158,9 +158,7 @@ public class GenericMessagingTemplateTests {
SubscribableChannel channel = mock(SubscribableChannel.class);
MessageHandler handler = createLateReplier(latch, failure);
doAnswer(invocation -> {
this.executor.execute(() -> {
handler.handleMessage(invocation.getArgument(0));
});
this.executor.execute(() -> handler.handleMessage(invocation.getArgument(0)));
return true;
}).when(channel).send(any(Message.class), anyLong());
@@ -192,9 +190,7 @@ public class GenericMessagingTemplateTests {
SubscribableChannel channel = mock(SubscribableChannel.class);
MessageHandler handler = createLateReplier(latch, failure);
doAnswer(invocation -> {
this.executor.execute(() -> {
handler.handleMessage(invocation.getArgument(0));
});
this.executor.execute(() -> handler.handleMessage(invocation.getArgument(0)));
return true;
}).when(channel).send(any(Message.class), anyLong());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,9 +19,7 @@ package org.springframework.messaging.core;
import java.io.Writer;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.messaging.Message;
@@ -29,7 +27,7 @@ import org.springframework.messaging.converter.GenericMessageConverter;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.messaging.support.GenericMessage;
import static org.hamcrest.CoreMatchers.*;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
/**
@@ -40,9 +38,6 @@ import static org.junit.Assert.*;
*/
public class MessageReceivingTemplateTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
private TestMessagingTemplate template;
@@ -104,9 +99,9 @@ public class MessageReceivingTemplateTests {
this.template.setReceiveMessage(expected);
this.template.setMessageConverter(new GenericMessageConverter());
thrown.expect(MessageConversionException.class);
thrown.expectCause(isA(ConversionFailedException.class));
this.template.receiveAndConvert("somewhere", Integer.class);
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
this.template.receiveAndConvert("somewhere", Integer.class))
.withCauseInstanceOf(ConversionFailedException.class);
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@ import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TestName;
import org.springframework.beans.factory.support.StaticListableBeanFactory;
@@ -47,6 +46,7 @@ import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.*;
/**
@@ -59,9 +59,6 @@ public class DefaultMessageHandlerMethodFactoryTests {
@Rule
public final TestName name = new TestName();
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void customConversion() throws Exception {
@@ -95,8 +92,8 @@ public class DefaultMessageHandlerMethodFactoryTests {
InvocableHandlerMethod invocableHandlerMethod =
createInvocableHandlerMethod(instance, "simpleString", String.class);
thrown.expect(MessageConversionException.class);
invocableHandlerMethod.invoke(MessageBuilder.withPayload(123).build());
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
invocableHandlerMethod.invoke(MessageBuilder.withPayload(123).build()));
}
@Test
@@ -109,8 +106,8 @@ public class DefaultMessageHandlerMethodFactoryTests {
InvocableHandlerMethod invocableHandlerMethod =
createInvocableHandlerMethod(instance, "simpleString", String.class);
thrown.expect(MessageConversionException.class);
invocableHandlerMethod.invoke(MessageBuilder.withPayload(123).build());
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
invocableHandlerMethod.invoke(MessageBuilder.withPayload(123).build()));
}
@Test
@@ -148,9 +145,8 @@ public class DefaultMessageHandlerMethodFactoryTests {
InvocableHandlerMethod invocableHandlerMethod2 =
createInvocableHandlerMethod(instance, "simpleString", String.class);
thrown.expect(MethodArgumentResolutionException.class);
thrown.expectMessage("No suitable resolver");
invocableHandlerMethod2.invoke(message);
assertThatExceptionOfType(MethodArgumentResolutionException.class).isThrownBy(() ->
invocableHandlerMethod2.invoke(message)).withMessageContaining("No suitable resolver");
}
@Test
@@ -184,8 +180,8 @@ public class DefaultMessageHandlerMethodFactoryTests {
InvocableHandlerMethod invocableHandlerMethod =
createInvocableHandlerMethod(instance, "payloadValidation", String.class);
thrown.expect(MethodArgumentNotValidException.class);
invocableHandlerMethod.invoke(MessageBuilder.withPayload("failure").build());
assertThatExceptionOfType(MethodArgumentNotValidException.class).isThrownBy(() ->
invocableHandlerMethod.invoke(MessageBuilder.withPayload("failure").build()));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,9 +20,7 @@ import java.lang.reflect.Method;
import java.util.Locale;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.MethodParameter;
import org.springframework.messaging.Message;
@@ -33,6 +31,7 @@ import org.springframework.messaging.support.ErrorMessage;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.messaging.support.MessageBuilder;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
@@ -44,9 +43,6 @@ import static org.mockito.Mockito.*;
*/
public class MessageMethodArgumentResolverTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
private MessageConverter converter;
private MessageMethodArgumentResolver resolver;
@@ -112,10 +108,10 @@ public class MessageMethodArgumentResolverTests {
MethodParameter parameter = new MethodParameter(this.method, 1);
assertTrue(this.resolver.supportsParameter(parameter));
thrown.expect(MessageConversionException.class);
thrown.expectMessage(Integer.class.getName());
thrown.expectMessage(String.class.getName());
this.resolver.resolveArgument(parameter, message);
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
this.resolver.resolveArgument(parameter, message))
.withMessageContaining(Integer.class.getName())
.withMessageContaining(String.class.getName());
}
@Test
@@ -124,11 +120,11 @@ public class MessageMethodArgumentResolverTests {
MethodParameter parameter = new MethodParameter(this.method, 1);
assertTrue(this.resolver.supportsParameter(parameter));
thrown.expect(MessageConversionException.class);
thrown.expectMessage("payload is empty");
thrown.expectMessage(Integer.class.getName());
thrown.expectMessage(String.class.getName());
this.resolver.resolveArgument(parameter, message);
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
this.resolver.resolveArgument(parameter, message))
.withMessageContaining("payload is empty")
.withMessageContaining(Integer.class.getName())
.withMessageContaining(String.class.getName());
}
@Test
@@ -146,10 +142,10 @@ public class MessageMethodArgumentResolverTests {
MethodParameter parameter = new MethodParameter(this.method, 3);
assertTrue(this.resolver.supportsParameter(parameter));
thrown.expect(MessageConversionException.class);
thrown.expectMessage(Number.class.getName());
thrown.expectMessage(Locale.class.getName());
this.resolver.resolveArgument(parameter, message);
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
this.resolver.resolveArgument(parameter, message))
.withMessageContaining(Number.class.getName())
.withMessageContaining(Locale.class.getName());
}
@Test
@@ -177,10 +173,10 @@ public class MessageMethodArgumentResolverTests {
MethodParameter parameter = new MethodParameter(this.method, 4);
assertTrue(this.resolver.supportsParameter(parameter));
thrown.expect(MethodArgumentTypeMismatchException.class);
thrown.expectMessage(ErrorMessage.class.getName());
thrown.expectMessage(GenericMessage.class.getName());
assertSame(message, this.resolver.resolveArgument(parameter, message));
assertThatExceptionOfType(MethodArgumentTypeMismatchException.class).isThrownBy(() ->
this.resolver.resolveArgument(parameter, message))
.withMessageContaining(ErrorMessage.class.getName())
.withMessageContaining(GenericMessage.class.getName());
}
@Test
@@ -202,10 +198,10 @@ public class MessageMethodArgumentResolverTests {
MethodParameter parameter = new MethodParameter(this.method, 1);
assertTrue(this.resolver.supportsParameter(parameter));
thrown.expect(MessageConversionException.class);
thrown.expectMessage(Integer.class.getName());
thrown.expectMessage(String.class.getName());
this.resolver.resolveArgument(parameter, message);
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
this.resolver.resolveArgument(parameter, message))
.withMessageContaining(Integer.class.getName())
.withMessageContaining(String.class.getName());
}
@Test
@@ -216,11 +212,11 @@ public class MessageMethodArgumentResolverTests {
MethodParameter parameter = new MethodParameter(this.method, 1);
assertTrue(this.resolver.supportsParameter(parameter));
thrown.expect(MessageConversionException.class);
thrown.expectMessage("payload is empty");
thrown.expectMessage(Integer.class.getName());
thrown.expectMessage(String.class.getName());
this.resolver.resolveArgument(parameter, message);
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
this.resolver.resolveArgument(parameter, message))
.withMessageContaining("payload is empty")
.withMessageContaining(Integer.class.getName())
.withMessageContaining(String.class.getName());
}
@Test // SPR-16486

View File

@@ -24,9 +24,7 @@ import java.lang.reflect.Method;
import java.util.Locale;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.core.MethodParameter;
@@ -40,6 +38,8 @@ import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.*;
/**
@@ -68,10 +68,6 @@ public class PayloadMethodArgumentResolverTests {
private MethodParameter paramValidated;
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Before
public void setup() throws Exception {
this.resolver = new PayloadMethodArgumentResolver(new StringMessageConverter(), testValidator());
@@ -113,17 +109,17 @@ public class PayloadMethodArgumentResolverTests {
@Test
public void resolveRequiredEmpty() throws Exception {
Message<?> message = MessageBuilder.withPayload("").build();
thrown.expect(MethodArgumentNotValidException.class); // required but empty
this.resolver.resolveArgument(paramAnnotated, message);
// required but empty
assertThatExceptionOfType(MethodArgumentNotValidException.class).isThrownBy(() ->
this.resolver.resolveArgument(paramAnnotated, message));
}
@Test
public void resolveRequiredEmptyNonAnnotatedParameter() throws Exception {
Message<?> message = MessageBuilder.withPayload("").build();
thrown.expect(MethodArgumentNotValidException.class); // required but empty
this.resolver.resolveArgument(this.paramNotAnnotated, message);
// required but empty
assertThatExceptionOfType(MethodArgumentNotValidException.class).isThrownBy(() ->
this.resolver.resolveArgument(this.paramNotAnnotated, message));
}
@Test
@@ -142,17 +138,17 @@ public class PayloadMethodArgumentResolverTests {
public void resolveNonConvertibleParam() throws Exception {
Message<?> notEmptyMessage = MessageBuilder.withPayload(123).build();
thrown.expect(MessageConversionException.class);
thrown.expectMessage("Cannot convert");
this.resolver.resolveArgument(this.paramAnnotatedRequired, notEmptyMessage);
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
this.resolver.resolveArgument(this.paramAnnotatedRequired, notEmptyMessage))
.withMessageContaining("Cannot convert");
}
@Test
public void resolveSpelExpressionNotSupported() throws Exception {
Message<?> message = MessageBuilder.withPayload("ABC".getBytes()).build();
thrown.expect(IllegalStateException.class);
this.resolver.resolveArgument(paramWithSpelExpression, message);
assertThatIllegalStateException().isThrownBy(() ->
this.resolver.resolveArgument(paramWithSpelExpression, message));
}
@Test
@@ -166,16 +162,16 @@ public class PayloadMethodArgumentResolverTests {
// See testValidator()
Message<?> message = MessageBuilder.withPayload("invalidValue".getBytes()).build();
thrown.expect(MethodArgumentNotValidException.class);
this.resolver.resolveArgument(this.paramValidated, message);
assertThatExceptionOfType(MethodArgumentNotValidException.class).isThrownBy(() ->
this.resolver.resolveArgument(this.paramValidated, message));
}
@Test
public void resolveFailValidationNoConversionNecessary() throws Exception {
Message<?> message = MessageBuilder.withPayload("invalidValue").build();
thrown.expect(MethodArgumentNotValidException.class);
this.resolver.resolveArgument(this.paramValidated, message);
assertThatExceptionOfType(MethodArgumentNotValidException.class).isThrownBy(() ->
this.resolver.resolveArgument(this.paramValidated, message));
}
@Test
@@ -184,8 +180,8 @@ public class PayloadMethodArgumentResolverTests {
assertEquals("ABC", this.resolver.resolveArgument(this.paramNotAnnotated, notEmptyMessage));
Message<?> emptyStringMessage = MessageBuilder.withPayload("").build();
thrown.expect(MethodArgumentNotValidException.class);
this.resolver.resolveArgument(this.paramValidated, emptyStringMessage);
assertThatExceptionOfType(MethodArgumentNotValidException.class).isThrownBy(() ->
this.resolver.resolveArgument(this.paramValidated, emptyStringMessage));
}
@Test
@@ -193,9 +189,9 @@ public class PayloadMethodArgumentResolverTests {
// See testValidator()
Message<?> message = MessageBuilder.withPayload("invalidValue".getBytes()).build();
thrown.expect(MethodArgumentNotValidException.class);
thrown.expectMessage("invalid value");
assertEquals("invalidValue", this.resolver.resolveArgument(this.paramValidatedNotAnnotated, message));
assertThatExceptionOfType(MethodArgumentNotValidException.class).isThrownBy(() ->
assertEquals("invalidValue", this.resolver.resolveArgument(this.paramValidatedNotAnnotated, message)))
.withMessageContaining("invalid value");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,14 +21,13 @@ import java.util.concurrent.ConcurrentHashMap;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.messaging.support.MessageBuilder;
import static org.assertj.core.api.Assertions.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
@@ -43,9 +42,6 @@ public class SimpAttributesContextHolderTests {
private SimpAttributes simpAttributes;
@Rule
public ExpectedException thrown = ExpectedException.none();
@Before
public void setUp() {
@@ -108,19 +104,19 @@ public class SimpAttributesContextHolderTests {
@Test
public void setAttributesFromMessageWithMissingSessionId() {
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage(startsWith("No session id in"));
SimpAttributesContextHolder.setAttributesFromMessage(new GenericMessage<Object>(""));
assertThatIllegalStateException().isThrownBy(() ->
SimpAttributesContextHolder.setAttributesFromMessage(new GenericMessage<Object>("")))
.withMessageStartingWith("No session id in");
}
@Test
public void setAttributesFromMessageWithMissingSessionAttributes() {
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage(startsWith("No session attributes in"));
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create();
headerAccessor.setSessionId("session1");
Message<?> message = MessageBuilder.createMessage("", headerAccessor.getMessageHeaders());
SimpAttributesContextHolder.setAttributesFromMessage(message);
assertThatIllegalStateException().isThrownBy(() ->
SimpAttributesContextHolder.setAttributesFromMessage(message))
.withMessageStartingWith("No session attributes in");
}
@Test
@@ -131,9 +127,9 @@ public class SimpAttributesContextHolderTests {
@Test
public void currentAttributesNone() {
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage(startsWith("No thread-bound SimpAttributes found"));
SimpAttributesContextHolder.currentAttributes();
assertThatIllegalStateException().isThrownBy(() ->
SimpAttributesContextHolder.currentAttributes())
.withMessageStartingWith("No thread-bound SimpAttributes found");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,12 +20,11 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;
@@ -42,9 +41,6 @@ public class SimpAttributesTests {
private Map<String, Object> map;
@Rule
public ExpectedException thrown = ExpectedException.none();
@Before
public void setup() {
@@ -82,9 +78,9 @@ public class SimpAttributesTests {
@Test
public void registerDestructionCallbackAfterSessionCompleted() {
this.simpAttributes.sessionCompleted();
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage(containsString("already completed"));
this.simpAttributes.registerDestructionCallback("name1", Mockito.mock(Runnable.class));
assertThatIllegalStateException().isThrownBy(() ->
this.simpAttributes.registerDestructionCallback("name1", Mockito.mock(Runnable.class)))
.withMessageContaining("already completed");
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@ import org.mockito.Mockito;
import org.springframework.beans.factory.ObjectFactory;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.BDDMockito.*;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -55,6 +55,7 @@ import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.util.MimeType;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.BDDMockito.*;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -70,7 +70,7 @@ import org.springframework.validation.annotation.Validated;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.*;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@ import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.util.MimeType;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.BDDMockito.*;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,8 +30,7 @@ import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.MultiValueMap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;
/**
* Test fixture for

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@ import org.springframework.messaging.support.MessageBuilder;
import org.springframework.scheduling.TaskScheduler;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,9 +25,7 @@ import org.junit.Test;
import org.springframework.messaging.Message;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
/**
* Unit tests for {@link BufferingStompDecoder}.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,11 +22,8 @@ import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.atomic.AtomicReference;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
@@ -46,10 +43,12 @@ import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.concurrent.SettableListenableFuture;
import static org.assertj.core.api.Assertions.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.any;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
/**
@@ -72,9 +71,6 @@ public class DefaultStompSessionTests {
@Captor
private ArgumentCaptor<Message<byte[]>> messageCaptor;
@Rule
public ExpectedException expected = ExpectedException.none();
@Before
public void setUp() {
@@ -399,9 +395,8 @@ public class DefaultStompSessionTests {
stompHeaders.setContentType(MimeTypeUtils.APPLICATION_JSON);
String payload = "{'foo':'bar'}";
this.expected.expect(MessageConversionException.class);
this.session.send(stompHeaders, payload);
verifyNoMoreInteractions(this.connection);
assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
this.session.send(stompHeaders, payload));
}
@Test
@@ -414,12 +409,9 @@ public class DefaultStompSessionTests {
future.setException(exception);
when(this.connection.send(any())).thenReturn(future);
this.expected.expect(MessageDeliveryException.class);
this.expected.expectCause(Matchers.sameInstance(exception));
this.session.send("/topic/foo", "sample payload".getBytes(StandardCharsets.UTF_8));
verifyNoMoreInteractions(this.connection);
assertThatExceptionOfType(MessageDeliveryException.class).isThrownBy(() ->
this.session.send("/topic/foo", "sample payload".getBytes(StandardCharsets.UTF_8)))
.withCause(exception);
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,9 +15,6 @@
*/
package org.springframework.messaging.simp.stomp;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -44,6 +41,9 @@ import org.springframework.messaging.tcp.TcpOperations;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureTask;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/**
* Unit tests for StompBrokerRelayMessageHandler.
*

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,8 +26,7 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.util.InvalidMimeTypeException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.*;
/**
* Test fixture for {@link StompDecoder}.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,8 +21,7 @@ import org.junit.Test;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
/**
* Test fixture for {@link StompEncoder}.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,11 +16,6 @@
package org.springframework.messaging.simp.user;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
@@ -35,6 +30,11 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.converter.MessageConverter;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/**
* Unit tests for {@link MultiServerUserRegistry}.
*

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,6 @@
package org.springframework.messaging.simp.user;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
@@ -41,6 +38,10 @@ import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.broker.BrokerAvailabilityEvent;
import org.springframework.scheduling.TaskScheduler;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
/**
* User tests for {@link UserRegistryMessageHandler}.
* @author Rossen Stoyanchev

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@ package org.springframework.messaging.support;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.MatcherAssert.*;
/**
* @author Gary Russell

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,9 +19,7 @@ package org.springframework.messaging.support;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
@@ -33,6 +31,7 @@ import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageDeliveryException;
import org.springframework.messaging.MessageHandler;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
@@ -45,9 +44,6 @@ import static org.mockito.BDDMockito.*;
*/
public class ExecutorSubscribableChannelTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
private ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
@Mock
@@ -69,9 +65,9 @@ public class ExecutorSubscribableChannelTests {
@Test
public void messageMustNotBeNull() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Message must not be null");
this.channel.send(null);
assertThatIllegalArgumentException().isThrownBy(() ->
this.channel.send(null))
.withMessageContaining("Message must not be null");
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,14 +21,13 @@ import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.util.IdGenerator;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.*;
/**
@@ -37,10 +36,6 @@ import static org.junit.Assert.*;
*/
public class MessageBuilderTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void testSimpleMessageCreation() {
Message<String> message = MessageBuilder.withPayload("foo").build();
@@ -192,9 +187,9 @@ public class MessageBuilderTests {
MessageHeaders headers = accessor.getMessageHeaders();
Message<?> message = MessageBuilder.createMessage("foo", headers);
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Already immutable");
accessor.setHeader("foo", "bar");
assertThatIllegalStateException().isThrownBy(() ->
accessor.setHeader("foo", "bar"))
.withMessageContaining("Already immutable");
assertSame(accessor, MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,24 +22,17 @@ import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.SerializationTestUtils;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
/**
* Test fixture for {@link MessageHeaderAccessor}.
@@ -50,10 +43,6 @@ import static org.junit.Assert.assertTrue;
*/
public class MessageHeaderAccessorTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void newEmptyHeaders() {
MessageHeaderAccessor accessor = new MessageHeaderAccessor();
@@ -217,13 +206,13 @@ public class MessageHeaderAccessorTests {
MessageHeaders headers = accessor.getMessageHeaders();
Message<?> message = MessageBuilder.createMessage("payload", headers);
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Already immutable");
accessor.setLeaveMutable(true);
assertThatIllegalStateException().isThrownBy(() ->
accessor.setLeaveMutable(true))
.withMessageContaining("Already immutable");
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Already immutable");
accessor.setHeader("foo", "baz");
assertThatIllegalStateException().isThrownBy(() ->
accessor.setHeader("foo", "baz"))
.withMessageContaining("Already immutable");
assertEquals("bar", headers.get("foo"));
assertSame(accessor, MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,14 +22,13 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.messaging.Message;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.*;
/**
@@ -39,10 +38,6 @@ import static org.junit.Assert.*;
*/
public class NativeMessageHeaderAccessorTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void createFromNativeHeaderMap() {
MultiValueMap<String, String> inputNativeHeaders = new LinkedMultiValueMap<>();
@@ -166,9 +161,9 @@ public class NativeMessageHeaderAccessorTests {
headerAccessor.setNativeHeader("foo", "bar");
headerAccessor.setImmutable();
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Already immutable");
headerAccessor.setNativeHeader("foo", "baz");
assertThatIllegalStateException().isThrownBy(() ->
headerAccessor.setNativeHeader("foo", "baz"))
.withMessageContaining("Already immutable");
}
@Test
@@ -216,9 +211,9 @@ public class NativeMessageHeaderAccessorTests {
headerAccessor.addNativeHeader("foo", "bar");
headerAccessor.setImmutable();
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Already immutable");
headerAccessor.addNativeHeader("foo", "baz");
assertThatIllegalStateException().isThrownBy(() ->
headerAccessor.addNativeHeader("foo", "baz"))
.withMessageContaining("Already immutable");
}
@Test