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

@@ -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: "));
}
}