Migrate tests to AssertJ

Mostly thanks to IDEA's plugin: https://plugins.jetbrains.com/plugin/10345-assertions2assertj
There is still a lot of work to do when complex and composite matchers are used.

* Add `awaitility` dependency and deprecate `EventuallyMatcher` in favor
of `awaitility`
* Remove Hamcrest from dependencies and disable JUnit & Hamcrest
static imports to encourage to use only AssertJ
* Migrate JUnit assumptions in rules to AssertJ's assumptions
* Deprecate some custom matchers in favor of existing in Hamcrest
after upgrading the last to version `2.1`
* Replace `ExpectedException` rules with `assertThatThrownBy()`
* Mention `MessagePredicate` in the `testing.adoc`
This commit is contained in:
Artem Bilan
2019-02-20 12:28:44 -05:00
parent b62c2a8fb3
commit 622d42c71a
916 changed files with 19714 additions and 21769 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-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,7 +20,6 @@ import java.util.function.Supplier;
import org.hamcrest.Description;
import org.hamcrest.DiagnosingMatcher;
import org.hamcrest.Factory;
import org.springframework.util.ObjectUtils;
@@ -29,7 +28,7 @@ import org.springframework.util.ObjectUtils;
* wrapped by the {@link java.util.function.Supplier}
*
* The goal is to defer the computation until the matcher needs to be actually evaluated.
* Mainly useful in conjunction with retrying matchers such as {@link EventuallyMatcher}
* Mainly useful in conjunction with retrying matchers such as {@code EventuallyMatcher}
*
* @author Marius Bogoevici
* @author Artem Bilan
@@ -53,7 +52,6 @@ public class EqualsResultMatcher<U> extends DiagnosingMatcher<U> {
public void describeTo(Description description) {
}
@Factory
public static <U> EqualsResultMatcher<U> equalsResult(Supplier<U> supplier) {
return new EqualsResultMatcher<>(supplier);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-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,7 +18,6 @@ package org.springframework.integration.test.matcher;
import org.hamcrest.Description;
import org.hamcrest.DiagnosingMatcher;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
@@ -31,7 +30,10 @@ import org.hamcrest.Matcher;
* @author Artem Bilan
*
* @since 4.2
*
* @deprecated since 5.2 in favor of <a href="https://github.com/awaitility/awaitility">Awaitility</a>
*/
@Deprecated
public class EventuallyMatcher<U> extends DiagnosingMatcher<U> {
private final Matcher<U> delegate;
@@ -50,12 +52,10 @@ public class EventuallyMatcher<U> extends DiagnosingMatcher<U> {
this.pause = pause;
}
@Factory
public static <U> Matcher<U> eventually(int nbAttempts, int pause, Matcher<U> delegate) {
return new EventuallyMatcher<>(delegate, nbAttempts, pause);
}
@Factory
public static <U> Matcher<U> eventually(Matcher<U> delegate) {
return new EventuallyMatcher<>(delegate);
}
@@ -69,7 +69,8 @@ public class EventuallyMatcher<U> extends DiagnosingMatcher<U> {
@Override
protected boolean matches(Object item, Description mismatchDescription) {
mismatchDescription.appendText(
String.format("failed after %d*%d=%dms:%n", this.nbAttempts, this.pause, this.nbAttempts * this.pause));
String.format("failed after %d*%d=%dms:%n", this.nbAttempts, this.pause,
this.nbAttempts * this.pause));
for (int i = 0; i < this.nbAttempts; i++) {
boolean result = this.delegate.matches(item);

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,10 +19,9 @@ package org.springframework.integration.test.matcher;
import java.util.Date;
import java.util.Map;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeMatcher;
import org.springframework.messaging.Message;
@@ -94,74 +93,60 @@ public class HeaderMatcher<T> extends TypeSafeMatcher<Message<T>> {
.appendDescriptionOf(this.matcher);
}
@Factory
public static <P, V> HeaderMatcher<P> hasHeader(String key, V value) {
return new HeaderMatcher<>(MapContentMatchers.hasEntry(key, value));
return new HeaderMatcher<>(Matchers.hasEntry(key, value));
}
@Factory
public static <P, V> HeaderMatcher<P> hasHeader(String key, Matcher<V> valueMatcher) {
return new HeaderMatcher<>(MapContentMatchers.hasEntry(key, valueMatcher));
return new HeaderMatcher<>(Matchers.hasEntry(Matchers.is(key), valueMatcher));
}
@Factory
public static <P> HeaderMatcher<P> hasHeaderKey(String key) {
return new HeaderMatcher<>(MapContentMatchers.hasKey(key));
return new HeaderMatcher<>(Matchers.hasKey(key));
}
@Factory
public static <P> HeaderMatcher<P> hasAllHeaders(Map<String, ?> entries) {
return new HeaderMatcher<>(MapContentMatchers.hasAllEntries(entries));
}
@Factory
public static <P, V> HeaderMatcher<P> hasMessageId(V value) {
return new HeaderMatcher<>(MapContentMatchers.hasEntry(MessageHeaders.ID, value));
return new HeaderMatcher<>(Matchers.hasEntry(MessageHeaders.ID, value));
}
@Factory
public static <P, V> HeaderMatcher<P> hasCorrelationId(V value) {
return new HeaderMatcher<>(MapContentMatchers.hasEntry("correlationId", value));
return new HeaderMatcher<>(Matchers.hasEntry("correlationId", value));
}
@Factory
public static <P> HeaderMatcher<P> hasSequenceNumber(Integer value) {
return hasSequenceNumber(CoreMatchers.is(value));
return hasSequenceNumber(Matchers.is(value));
}
@Factory
public static <P> HeaderMatcher<P> hasSequenceNumber(Matcher<Integer> matcher) {
return new HeaderMatcher<>(MapContentMatchers.hasEntry("sequenceNumber", matcher));
return new HeaderMatcher<>(Matchers.hasEntry(Matchers.is("sequenceNumber"), matcher));
}
@Factory
public static <P> HeaderMatcher<P> hasSequenceSize(Integer value) {
return hasSequenceSize(CoreMatchers.is(value));
return hasSequenceSize(Matchers.is(value));
}
@Factory
public static <P> HeaderMatcher<P> hasSequenceSize(Matcher<Integer> value) {
return new HeaderMatcher<>(MapContentMatchers.hasEntry("sequenceSize", value));
return new HeaderMatcher<>(Matchers.hasEntry(Matchers.is("sequenceSize"), value));
}
@Factory
public static <P> HeaderMatcher<P> hasExpirationDate(Date value) {
return hasExpirationDate(CoreMatchers.is(value.getTime()));
return hasExpirationDate(Matchers.is(value.getTime()));
}
@Factory
public static <P> HeaderMatcher<P> hasExpirationDate(Matcher<Long> matcher) {
return new HeaderMatcher<>(MapContentMatchers.hasEntry("expirationDate", matcher));
return new HeaderMatcher<>(Matchers.hasEntry(Matchers.is("expirationDate"), matcher));
}
@Factory
public static <P> HeaderMatcher<P> hasTimestamp(Date value) {
return hasTimestamp(CoreMatchers.is(value.getTime()));
return hasTimestamp(Matchers.is(value.getTime()));
}
@Factory
public static <P> HeaderMatcher<P> hasTimestamp(Matcher<Long> matcher) {
return new HeaderMatcher<>(MapContentMatchers.hasEntry(MessageHeaders.TIMESTAMP, matcher));
return new HeaderMatcher<>(Matchers.hasEntry(Matchers.is(MessageHeaders.TIMESTAMP), matcher));
}
}

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.
@@ -21,7 +21,6 @@ import java.util.List;
import java.util.Map;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeMatcher;
@@ -92,33 +91,56 @@ public class MapContentMatchers<T, V> extends TypeSafeMatcher<Map<? super T, ? s
}
@Factory
public static <T, V> Matcher<Map<? super T, ? super V>> hasEntry(T key, V value) {
return new MapContentMatchers<>(key, value);
/**
* Create {@link Matcher} for map entry.
* @param key the key to check.
* @param value the value to check.
* @param <K> the key type.
* @param <V> the value type.
* @return the {@link Matcher} for map entry.
* @deprecated since 5.2 in favor of {@link Matchers#hasEntry(Object, Object)}.
*/
@Deprecated
public static <K, V> Matcher<Map<? extends K, ? extends V>> hasEntry(K key, V value) {
return Matchers.hasEntry(key, value);
}
@Factory
public static <T, V> Matcher<Map<? super T, ? super V>> hasEntry(T key, Matcher<V> valueMatcher) {
return new MapContentMatchers<>(key, valueMatcher);
/**
* Create {@link Matcher} for map entry.
* @param key the key to check.
* @param valueMatcher the {@link Matcher} for value.
* @param <T> the key type.
* @param <V> the value type.
* @return the {@link Matcher} for map entry.
* @deprecated since 5.2 in favor of {@link Matchers#hasEntry(Matcher, Matcher)}.
*/
@Deprecated
public static <T, V> Matcher<Map<? extends T, ? extends V>> hasEntry(T key, Matcher<V> valueMatcher) {
return Matchers.hasEntry(Matchers.is(key), valueMatcher);
}
@Factory
@SuppressWarnings("unchecked")
public static <T, V> Matcher<Map<? super T, ? super V>> hasKey(T key) {
return new MapContentMatchers<>(key, (Matcher<V>) Matchers.anything());
/**
* Create {@link Matcher} for map key.
* @param key the key to check.
* @param <T> the key type.
* @return {@link Matcher} for map key.
* @deprecated since 5.2 in favor of {@link Matchers#hasKey}.
*/
@Deprecated
public static <T> Matcher<Map<? extends T, ?>> hasKey(T key) {
return Matchers.hasKey(key);
}
@Factory
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T, V> Matcher<Map<? super T, ? super V>> hasAllEntries(Map<T, V> entries) {
List<Matcher<Map<? super T, ? super V>>> matchers = new ArrayList<>(entries.size());
public static <T, V> Matcher<Map<? extends T, ? extends V>> hasAllEntries(Map<T, V> entries) {
List<Matcher<? super Map<T, V>>> matchers = new ArrayList<>(entries.size());
for (Map.Entry<T, V> entry : entries.entrySet()) {
final V value = entry.getValue();
if (value instanceof Matcher<?>) {
matchers.add(hasEntry(entry.getKey(), (Matcher<V>) value));
matchers.add(Matchers.hasEntry(Matchers.is(entry.getKey()), (Matcher<V>) value));
}
else {
matchers.add(hasEntry(entry.getKey(), value));
matchers.add(Matchers.hasEntry(entry.getKey(), value));
}
}
//return AllOf.allOf(matchers); //Does not work with Hamcrest 1.3

View File

@@ -0,0 +1,81 @@
/*
* Copyright 2016-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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.test.matcher;
import java.util.HashMap;
import java.util.Map;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
/**
* Matcher to make assertions about message equality easier. Usage:
*
* <pre class="code">
* {@code
* &#064;Test
* public void testSomething() {
* Message<String> expected = ...;
* Message<String> result = ...;
* assertThat(result, sameExceptImmutableHeaders(expected));
* }
*
* &#064;Factory
* public static Matcher<Message<?>> sameExceptImmutableHeaders(Message<?> expected) {
* return new MessageMatcher(expected);
* }
* }
* </pre>
*
* @author Dave Syer
* @author Artem Bilan
*
*/
public class MessageMatcher extends BaseMatcher<Message<?>> {
private final Object payload;
private final Map<String, Object> headers;
public MessageMatcher(Message<?> operand) {
this.payload = operand.getPayload();
this.headers = getHeaders(operand);
}
private Map<String, Object> getHeaders(Message<?> operand) {
HashMap<String, Object> headers = new HashMap<>(operand.getHeaders());
headers.remove(MessageHeaders.ID);
headers.remove(MessageHeaders.TIMESTAMP);
return headers;
}
public boolean matches(Object arg) {
Message<?> input = (Message<?>) arg;
Map<String, Object> inputHeaders = getHeaders(input);
return input.getPayload().equals(this.payload) && inputHeaders.equals(this.headers);
}
public void describeTo(Description description) {
description.appendText("Headers match except ID and timestamp for payload: ")
.appendValue(this.payload).appendText(" and headers: ")
.appendValue(this.headers);
}
}

View File

@@ -21,7 +21,6 @@ import java.util.Map;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
@@ -58,7 +57,6 @@ public class PayloadAndHeaderMatcher<T> extends BaseMatcher<Message<?>> {
private final String[] ignoreKeys;
@Factory
public static <P> PayloadAndHeaderMatcher<P> sameExceptIgnorableHeaders(Message<P> expected, String... ignoreKeys) {
return new PayloadAndHeaderMatcher<>(expected, ignoreKeys);
}

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.
@@ -17,7 +17,6 @@
package org.springframework.integration.test.matcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.core.IsEqual;
@@ -82,12 +81,10 @@ public class PayloadMatcher<T> extends TypeSafeMatcher<Message<?>> {
}
@Factory
public static <P> PayloadMatcher<P> hasPayload(P payload) {
return new PayloadMatcher<>(IsEqual.equalTo(payload));
}
@Factory
public static <P> PayloadMatcher<P> hasPayload(Matcher<P> payloadMatcher) {
return new PayloadMatcher<>(payloadMatcher);
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.test.predicate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
/**
* Predicate to make assertions about message equality easier. Usage:
*
* <pre class="code">
* {@code
* &#064;Test
* public void testSomething() {
* Message<String> expected = ...;
* Message<String> result = ...;
* assertThat(result).matches(new MessagePredicate(expected));
* }
* }
* </pre>
*
* @author Artem Bilan
*
* @since 5.2
*/
public class MessagePredicate implements Predicate<Message<?>> {
private final Object payload;
private final Map<String, Object> headers;
private final List<String> ignoredHeaders =
new ArrayList<>(Arrays.asList(MessageHeaders.ID, MessageHeaders.TIMESTAMP));
public MessagePredicate(Message<?> operand, String... ignoredHeaders) {
this.payload = operand.getPayload();
if (ignoredHeaders != null) {
this.ignoredHeaders.addAll(Arrays.asList(ignoredHeaders));
}
this.headers = getHeaders(operand);
}
@Override
public boolean test(Message<?> input) {
Map<String, Object> inputHeaders = getHeaders(input);
return input.getPayload().equals(this.payload) && inputHeaders.equals(this.headers);
}
private Map<String, Object> getHeaders(Message<?> operand) {
HashMap<String, Object> headers = new HashMap<>(operand.getHeaders());
this.ignoredHeaders.forEach(headers::remove);
return headers;
}
}

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,9 +16,10 @@
package org.springframework.integration.test.support;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -78,17 +79,17 @@ public abstract class AbstractRequestResponseScenarioTests {
((SubscribableChannel) outputChannel).subscribe(scenario.getResponseValidator());
}
Assert.assertTrue(name + ": message not sent on " + scenario.getInputChannelName(),
inputChannel.send(scenario.getMessage()));
assertThat(inputChannel.send(scenario.getMessage()))
.as(name + ": message not sent on " + scenario.getInputChannelName()).isTrue();
if (outputChannel instanceof PollableChannel) {
Message<?> response = ((PollableChannel) outputChannel).receive(10000);
Assert.assertNotNull(name + ": receive timeout on " + scenario.getOutputChannelName(), response);
assertThat(response).as(name + ": receive timeout on " + scenario.getOutputChannelName()).isNotNull();
scenario.getResponseValidator().handleMessage(response);
}
Assert.assertNotNull("message was not handled on " + outputChannel + " for scenario '" + name + "'.",
scenario.getResponseValidator().getLastMessage());
assertThat(scenario.getResponseValidator().getLastMessage())
.as("message was not handled on " + outputChannel + " for scenario '" + name + "'.").isNotNull();
if (outputChannel instanceof SubscribableChannel) {
((SubscribableChannel) outputChannel).unsubscribe(scenario.getResponseValidator());

View File

@@ -0,0 +1,190 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.test.matcher;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
/**
* @author Alex Peters
* @author Iwein Fuld
* @author Gunnar Hillert
* @author Artem Bilan
*
*/
public class HeaderMatcherTests {
static final String UNKNOWN_KEY = "unknownKey";
static final String ANY_HEADER_VALUE = "bar";
static final String ANY_HEADER_KEY = "test.foo";
static final String ANY_PAYLOAD = "bla";
static final String OTHER_HEADER_KEY = "test.number";
static final Integer OTHER_HEADER_VALUE = 123;
Message<?> message;
@Before
public void setUp() {
message = MessageBuilder.withPayload(ANY_PAYLOAD)
.setHeader(ANY_HEADER_KEY, ANY_HEADER_VALUE)
.setHeader(OTHER_HEADER_KEY, OTHER_HEADER_VALUE).build();
}
@Test
public void hasEntry_withValidKeyValue_matches() {
Assert.assertThat(message, HeaderMatcher.hasHeader(ANY_HEADER_KEY, ANY_HEADER_VALUE));
Assert.assertThat(message, HeaderMatcher.hasHeader(OTHER_HEADER_KEY, OTHER_HEADER_VALUE));
}
@Test
public void hasEntry_withUnknownKey_notMatching() {
Assert.assertThat(message, Matchers.not(HeaderMatcher.hasHeader("test.unknown", ANY_HEADER_VALUE)));
}
@Test
public void hasEntry_withValidKeyAndMatcherValue_matches() {
Assert.assertThat(message,
HeaderMatcher.hasHeader(ANY_HEADER_KEY, Matchers.instanceOf(String.class)));
Assert.assertThat(message, HeaderMatcher.hasHeader(ANY_HEADER_KEY, Matchers.notNullValue()));
Assert.assertThat(message, HeaderMatcher.hasHeader(ANY_HEADER_KEY, Matchers.is(ANY_HEADER_VALUE)));
}
@Test
public void hasEntry_withValidKeyAndMatcherValue_notMatching() {
Assert.assertThat(message,
Matchers.not(HeaderMatcher.hasHeader(ANY_HEADER_KEY,
Matchers.is(Matchers.instanceOf(Integer.class)))));
}
@Test
public void hasKey_withValidKey_matches() {
Assert.assertThat(message, HeaderMatcher.hasHeaderKey(ANY_HEADER_KEY));
Assert.assertThat(message, HeaderMatcher.hasHeaderKey(OTHER_HEADER_KEY));
}
@Test
public void hasKey_withInvalidKey_notMatching() {
Assert.assertThat(message, Matchers.not(HeaderMatcher.hasHeaderKey(UNKNOWN_KEY)));
}
@Test
public void hasAllEntries_withMessageHeader_matches() {
Map<String, Object> expectedInHeaderMap = message.getHeaders();
Assert.assertThat(message, HeaderMatcher.hasAllHeaders(expectedInHeaderMap));
}
@Test
public void hasAllEntries_withValidKeyValueOrMatcherValue_matches() {
Map<String, Object> expectedInHeaderMap = new HashMap<>();
expectedInHeaderMap.put(ANY_HEADER_KEY, ANY_HEADER_VALUE);
expectedInHeaderMap.put(OTHER_HEADER_KEY, Matchers.is(OTHER_HEADER_VALUE));
Assert.assertThat(message, HeaderMatcher.hasAllHeaders(expectedInHeaderMap));
}
@Test
public void hasAllEntries_withInvalidValidKeyValueOrMatcherValue_notMatching() {
Map<String, Object> expectedInHeaderMap = new HashMap<>();
expectedInHeaderMap.put(ANY_HEADER_KEY, ANY_HEADER_VALUE); // valid
expectedInHeaderMap.put(UNKNOWN_KEY, Matchers.not(Matchers.nullValue())); // fails
Assert.assertThat(message, Matchers.not(HeaderMatcher.hasAllHeaders(expectedInHeaderMap)));
expectedInHeaderMap.remove(UNKNOWN_KEY);
expectedInHeaderMap.put(OTHER_HEADER_KEY, ANY_HEADER_VALUE); // fails
}
@Test
public void readableException_singleHeader() {
try {
Assert.assertThat(message, HeaderMatcher.hasHeader("corn", "bread"));
}
catch (AssertionError ae) {
Assert.assertThat(ae.getMessage(), Matchers.containsString("Expected: a Message with Headers containing "
));
}
}
@Test
public void readableException_allHeaders() {
try {
Map<String, String> entries = new HashMap<>();
entries.put("corn", "bread");
entries.put("chocolate", "pudding");
Assert.assertThat(message, HeaderMatcher.hasAllHeaders(entries));
}
catch (AssertionError ae) {
Assert.assertThat(ae.getMessage(), Matchers.containsString("Expected: a Message with Headers containing "
));
}
}
@Test
public void hasMessageId_sameId() {
Assert.assertThat(message, HeaderMatcher.hasMessageId(message.getHeaders().getId()));
}
@Test
public void hasCorrelationId_() {
UUID correlationId = message.getHeaders().getId();
message = MessageBuilder.withPayload("blabla").setHeader("correlationId", correlationId).build();
Assert.assertThat(message, HeaderMatcher.hasCorrelationId(correlationId));
}
@Test
public void hasSequenceNumber_() {
int sequenceNumber = 123;
message = MessageBuilder.fromMessage(message).setHeader("sequenceNumber", sequenceNumber).build();
Assert.assertThat(message, HeaderMatcher.hasSequenceNumber(sequenceNumber));
}
@Test
public void hasSequenceSize_() {
int sequenceSize = 123;
message = MessageBuilder.fromMessage(message).setHeader("sequenceSize", sequenceSize).build();
Assert.assertThat(message, HeaderMatcher.hasSequenceSize(sequenceSize));
Assert.assertThat(message, HeaderMatcher.hasSequenceSize(Matchers.is(sequenceSize)));
}
@Test
public void hasTimestamp_() {
Assert.assertThat(message, HeaderMatcher.hasTimestamp(new Date(message.getHeaders().getTimestamp())));
}
@Test
public void hasExpirationDate_() {
Assert.assertThat(message, Matchers.not(HeaderMatcher.hasExpirationDate(Matchers.any(Long.class))));
Date expirationDate = new Date(System.currentTimeMillis() + 10000);
message = MessageBuilder.fromMessage(message).setHeader("expirationDate", expirationDate.getTime()).build();
Assert.assertThat(message, HeaderMatcher.hasExpirationDate(expirationDate));
Assert.assertThat(message,
HeaderMatcher.hasExpirationDate(Matchers.not(Matchers.is((System.currentTimeMillis())))));
}
}

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,19 +16,11 @@
package org.springframework.integration.test.matcher;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.springframework.integration.test.matcher.MapContentMatchers.hasAllEntries;
import static org.springframework.integration.test.matcher.MapContentMatchers.hasEntry;
import static org.springframework.integration.test.matcher.MapContentMatchers.hasKey;
import java.util.HashMap;
import java.util.Map;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -53,70 +45,72 @@ public class MapContainsTests {
@Before
public void setUp() {
map = new HashMap<String, Object>();
map = new HashMap<>();
map.put(SOME_KEY, SOME_VALUE);
map.put(OTHER_KEY, OTHER_VALUE);
}
@Test
public void hasKey_validKey_matching() throws Exception {
assertThat(map, hasKey(SOME_KEY));
public void hasKey_validKey_matching() {
Assert.assertThat(map, Matchers.hasKey(SOME_KEY));
}
@Test
public void hasKey_unknownKey_notMatching() throws Exception {
assertThat(map, not(hasKey(UNKNOWN_KEY)));
public void hasKey_unknownKey_notMatching() {
Assert.assertThat(map, Matchers.not(Matchers.hasKey(UNKNOWN_KEY)));
}
@Test
public void hasEntry_withValidKeyValue_matches() throws Exception {
assertThat(map, hasEntry(SOME_KEY, SOME_VALUE));
assertThat(map, hasEntry(OTHER_KEY, OTHER_VALUE));
public void hasEntry_withValidKeyValue_matches() {
Assert.assertThat(map, Matchers.hasEntry(SOME_KEY, SOME_VALUE));
Assert.assertThat(map, Matchers.hasEntry(OTHER_KEY, OTHER_VALUE));
}
@Test
public void hasEntry_withUnknownKey_notMatching() throws Exception {
assertThat(map, not(hasEntry("test.unknown", SOME_VALUE)));
public void hasEntry_withUnknownKey_notMatching() {
Assert.assertThat(map, Matchers.not(Matchers.hasEntry("test.unknown", SOME_VALUE)));
}
@Test
public void hasEntry_withValidKeyAndMatcherValue_matches() throws Exception {
assertThat(map, hasEntry(SOME_KEY, is(instanceOf(String.class))));
assertThat(map, hasEntry(SOME_KEY, notNullValue()));
assertThat(map, hasEntry(SOME_KEY, is(SOME_VALUE)));
public void hasEntry_withValidKeyAndMatcherValue_matches() {
Assert.assertThat(map, Matchers.hasEntry(Matchers.is(SOME_KEY), Matchers.instanceOf(String.class)));
Assert.assertThat(map, Matchers.hasEntry(Matchers.is(SOME_KEY), Matchers.notNullValue()));
Assert.assertThat(map, Matchers.hasEntry(Matchers.is(SOME_KEY), Matchers.is(SOME_VALUE)));
}
@Test
public void hasEntry_withValidKeyAndMatcherValue_notMatching() throws Exception {
assertThat(map, not(hasEntry(SOME_KEY, is(instanceOf(Integer.class)))));
public void hasEntry_withValidKeyAndMatcherValue_notMatching() {
Assert.assertThat(map,
Matchers.not(Matchers.hasEntry(SOME_KEY, Matchers.is(Matchers.instanceOf(Integer.class)))));
}
@Test
public void hasEntry_withTypedValueMap_matches() throws Exception {
Map<String, String> map = new HashMap<String, String>();
public void hasEntry_withTypedValueMap_matches() {
Map<String, String> map = new HashMap<>();
map.put("a", "b");
map.put("c", "d");
assertThat(map, hasEntry("a", "b"));
assertThat(map, not(hasEntry(SOME_KEY, is("a"))));
assertThat(map, hasAllEntries(map));
Assert.assertThat(map, Matchers.hasEntry("a", "b"));
Assert.assertThat(map, Matchers.not(Matchers.hasEntry(SOME_KEY, Matchers.is("a"))));
Assert.assertThat(map, MapContentMatchers.hasAllEntries(map));
}
@Test
public void hasAllEntries_withValidKeyValueOrMatcherValue_matches() throws Exception {
Map<String, Object> expectedInHeaderMap = new HashMap<String, Object>();
public void hasAllEntries_withValidKeyValueOrMatcherValue_matches() {
Map<String, Object> expectedInHeaderMap = new HashMap<>();
expectedInHeaderMap.put(SOME_KEY, SOME_VALUE);
expectedInHeaderMap.put(OTHER_KEY, is(OTHER_VALUE));
assertThat(map, hasAllEntries(expectedInHeaderMap));
expectedInHeaderMap.put(OTHER_KEY, Matchers.is(OTHER_VALUE));
Assert.assertThat(map, MapContentMatchers.hasAllEntries(expectedInHeaderMap));
}
@Test
public void hasAllEntries_withInvalidValidKeyValueOrMatcherValue_notMatching() throws Exception {
Map<String, Object> expectedInHeaderMap = new HashMap<String, Object>();
public void hasAllEntries_withInvalidValidKeyValueOrMatcherValue_notMatching() {
Map<String, Object> expectedInHeaderMap = new HashMap<>();
expectedInHeaderMap.put(SOME_KEY, SOME_VALUE); // valid
expectedInHeaderMap.put(UNKNOWN_KEY, not(nullValue())); // fails
assertThat(map, not(hasAllEntries(expectedInHeaderMap)));
expectedInHeaderMap.put(UNKNOWN_KEY, Matchers.not(Matchers.nullValue())); // fails
Assert.assertThat(map, Matchers.not(MapContentMatchers.hasAllEntries(expectedInHeaderMap)));
expectedInHeaderMap.remove(UNKNOWN_KEY);
expectedInHeaderMap.put(OTHER_KEY, SOME_VALUE); // fails
}
}

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,17 +16,13 @@
package org.springframework.integration.test.matcher;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.integration.test.matcher.MockitoMessageMatchers.messageWithHeaderEntry;
import static org.springframework.integration.test.matcher.MockitoMessageMatchers.messageWithPayload;
import java.util.Date;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -50,8 +46,6 @@ public class MockitoMessageMatchersTests {
static final Date SOME_PAYLOAD = new Date();
static final String UNKNOWN_KEY = "unknownKey";
static final String SOME_HEADER_VALUE = "bar";
static final String SOME_HEADER_KEY = "test.foo";
@@ -71,28 +65,32 @@ public class MockitoMessageMatchersTests {
}
@Test
public void anyMatcher_withVerifyArgumentMatcherAndEqualPayload_matching() throws Exception {
public void anyMatcher_withVerifyArgumentMatcherAndEqualPayload_matching() {
handler.handleMessage(message);
verify(handler).handleMessage(messageWithPayload(SOME_PAYLOAD));
verify(handler).handleMessage(messageWithPayload(is(instanceOf(Date.class))));
verify(handler).handleMessage(MockitoMessageMatchers.messageWithPayload(SOME_PAYLOAD));
verify(handler)
.handleMessage(MockitoMessageMatchers.messageWithPayload(Matchers.is(Matchers.instanceOf(Date.class))));
}
@Test(expected = ArgumentsAreDifferent.class)
public void anyMatcher_withVerifyAndDifferentPayload_notMatching() throws Exception {
public void anyMatcher_withVerifyAndDifferentPayload_notMatching() {
handler.handleMessage(message);
verify(handler).handleMessage(messageWithPayload(nullValue()));
verify(handler).handleMessage(MockitoMessageMatchers.messageWithPayload(Matchers.nullValue()));
}
@Test
public void anyMatcher_withWhenArgumentMatcherAndEqualPayload_matching() throws Exception {
when(channel.send(messageWithPayload(SOME_PAYLOAD))).thenReturn(true);
assertThat(channel.send(message), is(true));
public void anyMatcher_withWhenArgumentMatcherAndEqualPayload_matching() {
when(channel.send(MockitoMessageMatchers.messageWithPayload(SOME_PAYLOAD))).thenReturn(true);
assertThat(channel.send(message)).isTrue();
}
@Test
public void anyMatcher_withWhenAndDifferentPayload_notMatching() throws Exception {
when(channel.send(messageWithHeaderEntry(SOME_HEADER_KEY, is(instanceOf(Short.class))))).thenReturn(true);
assertThat(channel.send(message), is(false));
public void anyMatcher_withWhenAndDifferentPayload_notMatching() {
when(channel.send(
MockitoMessageMatchers.messageWithHeaderEntry(SOME_HEADER_KEY,
Matchers.is(Matchers.instanceOf(Short.class)))))
.thenReturn(true);
assertThat(channel.send(message)).isFalse();
}
}

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,16 +16,10 @@
package org.springframework.integration.test.matcher;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.springframework.integration.test.matcher.PayloadMatcher.hasPayload;
import java.math.BigDecimal;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.messaging.Message;
@@ -44,33 +38,34 @@ public class PayloadMatcherTests {
private final Message<BigDecimal> message = MessageBuilder.withPayload(ANY_PAYLOAD).build();
@Test
public void hasPayload_withEqualValue_matches() throws Exception {
assertThat(this.message, hasPayload(new BigDecimal("1.123")));
public void hasPayload_withEqualValue_matches() {
Assert.assertThat(this.message, PayloadMatcher.hasPayload(new BigDecimal("1.123")));
}
@Test
public void hasPayload_withNotEqualValue_notMatching() throws Exception {
assertThat(this.message, not(hasPayload(new BigDecimal("456"))));
public void hasPayload_withNotEqualValue_notMatching() {
Assert.assertThat(this.message, Matchers.not(PayloadMatcher.hasPayload(new BigDecimal("456"))));
}
@Test
public void hasPayload_withMatcher_matches() throws Exception {
assertThat(this.message, hasPayload(is(instanceOf(BigDecimal.class))));
assertThat(this.message, hasPayload(notNullValue()));
public void hasPayload_withMatcher_matches() {
Assert.assertThat(this.message, PayloadMatcher.hasPayload(Matchers.is(Matchers.instanceOf(BigDecimal.class))));
Assert.assertThat(this.message, PayloadMatcher.hasPayload(Matchers.notNullValue()));
}
@Test
public void hasPayload_withNotMatchingMatcher_notMatching() throws Exception {
assertThat(this.message, not((hasPayload(is(instanceOf(String.class))))));
public void hasPayload_withNotMatchingMatcher_notMatching() {
Assert.assertThat(this.message,
Matchers.not((PayloadMatcher.hasPayload(Matchers.is(Matchers.instanceOf(String.class))))));
}
@Test
public void readableException() throws Exception {
public void readableException() {
try {
assertThat(this.message, hasPayload("woot"));
Assert.assertThat(this.message, PayloadMatcher.hasPayload("woot"));
}
catch (AssertionError ae) {
assertTrue(ae.getMessage().contains("Expected: a Message with payload: "));
Assert.assertThat(ae.getMessage(), Matchers.containsString("Expected: a Message with payload: "));
}
}