Skip to content

Commit 622d42c

Browse files
committed
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`
1 parent b62c2a8 commit 622d42c

File tree

916 files changed

+19736
-21791
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

916 files changed

+19736
-21791
lines changed

build.gradle

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ subprojects { subproject ->
9292
aspectjVersion = '1.9.2'
9393
assertjVersion = '3.12.0'
9494
assertkVersion = '0.13'
95+
awaitilityVersion = '3.1.6'
9596
boonVersion = '0.34'
9697
commonsDbcp2Version = '2.5.0'
9798
commonsIoVersion = '2.6'
@@ -102,7 +103,7 @@ subprojects { subproject ->
102103
googleJsr305Version = '3.0.2'
103104
groovyVersion = '2.5.6'
104105
guavaVersion = '26.0-jre'
105-
hamcrestVersion = '1.3'
106+
hamcrestVersion = '2.1'
106107
hazelcastVersion = '3.11.1'
107108
hibernateVersion = '5.4.1.Final'
108109
hsqldbVersion = '2.4.1'
@@ -159,12 +160,18 @@ subprojects { subproject ->
159160
// dependencies that are common across all java projects
160161
dependencies {
161162
if (!(subproject.name ==~ /.*-test.*/)) {
162-
testCompile project(":spring-integration-test-support")
163+
testCompile (project(":spring-integration-test-support")) {
164+
exclude group: 'org.hamcrest'
165+
}
163166
}
164167

165168
// JSR-305 only used for non-required meta-annotations
166-
compileOnly("com.google.code.findbugs:jsr305:$googleJsr305Version")
167-
testCompile("com.google.code.findbugs:jsr305:$googleJsr305Version")
169+
compileOnly "com.google.code.findbugs:jsr305:$googleJsr305Version"
170+
testCompile "com.google.code.findbugs:jsr305:$googleJsr305Version"
171+
172+
testCompile ("org.awaitility:awaitility:$awaitilityVersion") {
173+
exclude group: 'org.hamcrest'
174+
}
168175

169176
testCompile "org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion"
170177
testRuntime "org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion"
@@ -179,7 +186,7 @@ subprojects { subproject ->
179186
testRuntime "org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion"
180187
testRuntime "org.apache.logging.log4j:log4j-jcl:$log4jVersion"
181188

182-
testCompile("com.willowtreeapps.assertk:assertk-jvm:$assertkVersion")
189+
testCompile "com.willowtreeapps.assertk:assertk-jvm:$assertkVersion"
183190

184191
testCompile "org.jetbrains.kotlin:kotlin-reflect"
185192
testCompile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
@@ -330,9 +337,10 @@ subprojects { subproject ->
330337
project('spring-integration-test-support') {
331338
description = 'Spring Integration Test Support - **No SI Dependencies Allowed**'
332339
dependencies {
333-
compile "org.hamcrest:hamcrest-core:$hamcrestVersion"
334340
compile "org.hamcrest:hamcrest-library:$hamcrestVersion"
335-
compile "junit:junit:$junit4Version"
341+
compile ("junit:junit:$junit4Version") {
342+
exclude group: 'org.hamcrest'
343+
}
336344
compile ("org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion", optional)
337345
compileOnly 'org.apiguardian:apiguardian-api:1.0.0'
338346
compile "org.mockito:mockito-core:$mockitoVersion"
@@ -459,6 +467,7 @@ project('spring-integration-http') {
459467
compile ("com.rometools:rome:$romeToolsVersion", optional)
460468

461469
testCompile project(":spring-integration-security")
470+
testCompile "org.hamcrest:hamcrest-core:$hamcrestVersion"
462471
testCompile ("org.springframework.security:spring-security-config:$springSecurityVersion") {
463472
exclude group: 'org.springframework'
464473
}
@@ -674,7 +683,8 @@ project('spring-integration-webflux') {
674683
}
675684
compile "org.springframework:spring-webflux:$springVersion"
676685
compile ("io.projectreactor.netty:reactor-netty:$reactorNettyVersion" , optional)
677-
686+
687+
testCompile "org.hamcrest:hamcrest-core:$hamcrestVersion"
678688
testCompile "org.springframework:spring-webmvc:$springVersion"
679689
testCompile ("org.springframework.security:spring-security-config:$springSecurityVersion") {
680690
exclude group: 'org.springframework'

spring-integration-amqp/src/test/java/org/springframework/integration/amqp/channel/ChannelTests.java

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2018 the original author or authors.
2+
* Copyright 2014-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,13 +16,8 @@
1616

1717
package org.springframework.integration.amqp.channel;
1818

19-
import static org.hamcrest.Matchers.equalTo;
20-
import static org.hamcrest.Matchers.instanceOf;
21-
import static org.junit.Assert.assertEquals;
22-
import static org.junit.Assert.assertNotNull;
23-
import static org.junit.Assert.assertSame;
24-
import static org.junit.Assert.assertThat;
25-
import static org.junit.Assert.assertTrue;
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2621
import static org.mockito.ArgumentMatchers.any;
2722
import static org.mockito.BDDMockito.willThrow;
2823
import static org.mockito.Mockito.mock;
@@ -34,9 +29,7 @@
3429

3530
import org.junit.After;
3631
import org.junit.ClassRule;
37-
import org.junit.Rule;
3832
import org.junit.Test;
39-
import org.junit.rules.ExpectedException;
4033
import org.junit.runner.RunWith;
4134

4235
import org.springframework.amqp.core.AmqpTemplate;
@@ -80,9 +73,6 @@ public class ChannelTests {
8073
public static final BrokerRunning brokerIsRunning =
8174
BrokerRunning.isRunningWithEmptyQueues("pollableWithEP", "withEP", "testConvertFail");
8275

83-
@Rule
84-
public ExpectedException exception = ExpectedException.none();
85-
8676
@Autowired
8777
private PublishSubscribeAmqpChannel channel;
8878

@@ -136,8 +126,8 @@ public void pubSubLostConnectionTest() throws Exception {
136126
this.pubSubWithEP.destroy();
137127
this.withEP.destroy();
138128
this.pollableWithEP.destroy();
139-
assertEquals(0,
140-
TestUtils.getPropertyValue(connectionFactory, "connectionListener.delegates", Collection.class).size());
129+
assertThat(TestUtils.getPropertyValue(connectionFactory, "connectionListener.delegates", Collection.class)
130+
.size()).isEqualTo(0);
141131
}
142132

143133
@SuppressWarnings("unchecked")
@@ -147,7 +137,8 @@ private void waitForNewConsumer(PublishSubscribeAmqpChannel channel, BlockingQue
147137
final Object consumersMonitor = TestUtils.getPropertyValue(channel, "container.consumersMonitor");
148138
int n = 0;
149139
while (n++ < 100) {
150-
Set<BlockingQueueConsumer> consumers = TestUtils.getPropertyValue(channel, "container.consumers", Set.class);
140+
Set<BlockingQueueConsumer> consumers = TestUtils
141+
.getPropertyValue(channel, "container.consumers", Set.class);
151142
synchronized (consumersMonitor) {
152143
if (!consumers.isEmpty()) {
153144
BlockingQueueConsumer newConsumer = consumers.iterator().next();
@@ -158,7 +149,7 @@ private void waitForNewConsumer(PublishSubscribeAmqpChannel channel, BlockingQue
158149
}
159150
Thread.sleep(100);
160151
}
161-
assertTrue("Failed to restart consumer", n < 100);
152+
assertThat(n < 100).as("Failed to restart consumer").isTrue();
162153
}
163154

164155
/*
@@ -177,21 +168,21 @@ public void channelDeclarationTests() {
177168
channel.afterPropertiesSet();
178169
channel.onCreate(null);
179170

180-
assertNotNull(admin.getQueueProperties("implicit"));
171+
assertThat(admin.getQueueProperties("implicit")).isNotNull();
181172

182173
admin.deleteQueue("explicit");
183174
channel.setQueueName("explicit");
184175
channel.afterPropertiesSet();
185176
channel.onCreate(null);
186177

187-
assertNotNull(admin.getQueueProperties("explicit"));
178+
assertThat(admin.getQueueProperties("explicit")).isNotNull();
188179

189180
admin.deleteQueue("explicit");
190181
admin.declareQueue(new Queue("explicit", false)); // verify no declaration if exists with non-standard props
191182
channel.afterPropertiesSet();
192183
channel.onCreate(null);
193184

194-
assertNotNull(admin.getQueueProperties("explicit"));
185+
assertThat(admin.getQueueProperties("explicit")).isNotNull();
195186
admin.deleteQueue("explicit");
196187
}
197188

@@ -203,7 +194,7 @@ public void testAmqpChannelFactoryBean() throws Exception {
203194
channelFactoryBean.setBeanName("testChannel");
204195
channelFactoryBean.afterPropertiesSet();
205196
AbstractAmqpChannel channel = channelFactoryBean.getObject();
206-
assertThat(channel, instanceOf(PointToPointSubscribableAmqpChannel.class));
197+
assertThat(channel).isInstanceOf(PointToPointSubscribableAmqpChannel.class);
207198

208199
channelFactoryBean = new AmqpChannelFactoryBean();
209200
channelFactoryBean.setBeanFactory(mock(BeanFactory.class));
@@ -212,7 +203,7 @@ public void testAmqpChannelFactoryBean() throws Exception {
212203
channelFactoryBean.setPubSub(true);
213204
channelFactoryBean.afterPropertiesSet();
214205
channel = channelFactoryBean.getObject();
215-
assertThat(channel, instanceOf(PublishSubscribeAmqpChannel.class));
206+
assertThat(channel).isInstanceOf(PublishSubscribeAmqpChannel.class);
216207

217208
RabbitAdmin rabbitAdmin = new RabbitAdmin(this.connectionFactory);
218209
rabbitAdmin.deleteQueue("testChannel");
@@ -225,24 +216,24 @@ public void extractPayloadTests() {
225216
Message<?> message = MessageBuilder.withPayload(foo).setHeader("baz", "qux").build();
226217
this.pollableWithEP.send(message);
227218
Message<?> received = this.pollableWithEP.receive(10000);
228-
assertNotNull(received);
229-
assertThat(received.getPayload(), equalTo(foo));
230-
assertThat(received.getHeaders().get("baz"), equalTo("qux"));
219+
assertThat(received).isNotNull();
220+
assertThat(received.getPayload()).isEqualTo(foo);
221+
assertThat(received.getHeaders().get("baz")).isEqualTo("qux");
231222

232223
this.withEP.send(message);
233224
received = this.out.receive(10000);
234-
assertNotNull(received);
235-
assertThat(received.getPayload(), equalTo(foo));
236-
assertThat(received.getHeaders().get("baz"), equalTo("qux"));
225+
assertThat(received).isNotNull();
226+
assertThat(received.getPayload()).isEqualTo(foo);
227+
assertThat(received.getHeaders().get("baz")).isEqualTo("qux");
237228

238229
this.pubSubWithEP.send(message);
239230
received = this.out.receive(10000);
240-
assertNotNull(received);
241-
assertThat(received.getPayload(), equalTo(foo));
242-
assertThat(received.getHeaders().get("baz"), equalTo("qux"));
231+
assertThat(received).isNotNull();
232+
assertThat(received.getPayload()).isEqualTo(foo);
233+
assertThat(received.getHeaders().get("baz")).isEqualTo("qux");
243234

244-
assertSame(this.mapperIn, TestUtils.getPropertyValue(this.pollableWithEP, "inboundHeaderMapper"));
245-
assertSame(this.mapperOut, TestUtils.getPropertyValue(this.pollableWithEP, "outboundHeaderMapper"));
235+
assertThat(TestUtils.getPropertyValue(this.pollableWithEP, "inboundHeaderMapper")).isSameAs(this.mapperIn);
236+
assertThat(TestUtils.getPropertyValue(this.pollableWithEP, "outboundHeaderMapper")).isSameAs(this.mapperOut);
246237
}
247238

248239
@Test
@@ -257,9 +248,9 @@ public void messageConversionTests() {
257248
MessageListener.class);
258249
willThrow(new MessageConversionException("foo", new IllegalStateException("bar")))
259250
.given(messageConverter).fromMessage(any(org.springframework.amqp.core.Message.class));
260-
this.exception.expect(MessageConversionException.class);
261-
this.exception.expectCause(instanceOf(IllegalStateException.class));
262-
listener.onMessage(mock(org.springframework.amqp.core.Message.class));
251+
assertThatThrownBy(() -> listener.onMessage(mock(org.springframework.amqp.core.Message.class)))
252+
.isInstanceOf(MessageConversionException.class)
253+
.hasCauseInstanceOf(IllegalStateException.class);
263254
}
264255

265256
public static class Foo {

spring-integration-amqp/src/test/java/org/springframework/integration/amqp/channel/DispatcherHasNoSubscribersTests.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,8 @@
1616

1717
package org.springframework.integration.amqp.channel;
1818

19-
import static org.hamcrest.Matchers.containsString;
20-
import static org.junit.Assert.assertNotNull;
21-
import static org.junit.Assert.assertThat;
22-
import static org.junit.Assert.assertTrue;
23-
import static org.junit.Assert.fail;
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.fail;
2421
import static org.mockito.ArgumentMatchers.any;
2522
import static org.mockito.ArgumentMatchers.anyBoolean;
2623
import static org.mockito.ArgumentMatchers.anyString;
@@ -87,8 +84,8 @@ public void testPtP() throws Exception {
8784
fail("Exception expected");
8885
}
8986
catch (MessageDeliveryException e) {
90-
assertThat(e.getMessage(),
91-
containsString("Dispatcher has no subscribers for amqp-channel 'noSubscribersChannel'."));
87+
assertThat(e.getMessage())
88+
.contains("Dispatcher has no subscribers for amqp-channel 'noSubscribersChannel'.");
9289
}
9390
}
9491

@@ -135,19 +132,18 @@ private List<String> insertMockLoggerInListener(
135132
}
136133

137134
private void verifyLogReceived(final List<String> logList) {
138-
assertTrue("Failed to get expected exception", logList.size() > 0);
135+
assertThat(logList.size() > 0).as("Failed to get expected exception").isTrue();
139136
boolean expectedExceptionFound = false;
140137
while (logList.size() > 0) {
141138
String message = logList.remove(0);
142-
assertNotNull("Failed to get expected exception", message);
139+
assertThat(message).as("Failed to get expected exception").isNotNull();
143140
if (message.startsWith("Dispatcher has no subscribers")) {
144141
expectedExceptionFound = true;
145-
assertThat(message,
146-
containsString("Dispatcher has no subscribers for amqp-channel 'noSubscribersChannel'."));
142+
assertThat(message).contains("Dispatcher has no subscribers for amqp-channel 'noSubscribersChannel'.");
147143
break;
148144
}
149145
}
150-
assertTrue("Failed to get expected exception", expectedExceptionFound);
146+
assertThat(expectedExceptionFound).as("Failed to get expected exception").isTrue();
151147
}
152148

153149
}

0 commit comments

Comments
 (0)