Refactor AssertJ assertions into more idiomatic ones

This commit refactors some AssertJ assertions into more idiomatic and
readable ones. Using the dedicated assertion instead of a generic one
will produce more meaningful error messages. 

For instance, consider collection size:
```
// expected: 5 but was: 2
assertThat(collection.size()).equals(5);
// Expected size: 5 but was: 2 in: [1, 2]
assertThat(collection).hasSize(5);
```

Closes gh-30104
This commit is contained in:
Krzysztof Krasoń
2023-04-04 17:34:07 +02:00
committed by GitHub
parent dd97ee4e99
commit 1734deca1e
371 changed files with 3177 additions and 3076 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -122,7 +122,7 @@ public class MessageBrokerBeanDefinitionParserTests {
WebSocketHttpRequestHandler wsHttpRequestHandler = (WebSocketHttpRequestHandler) httpRequestHandler;
HandshakeHandler handshakeHandler = wsHttpRequestHandler.getHandshakeHandler();
assertThat(handshakeHandler).isNotNull();
assertThat(handshakeHandler instanceof TestHandshakeHandler).isTrue();
assertThat(handshakeHandler).isInstanceOf(TestHandshakeHandler.class);
List<HandshakeInterceptor> interceptors = wsHttpRequestHandler.getHandshakeInterceptors();
assertThat(interceptors).extracting("class").containsExactly(FooTestInterceptor.class,
BarTestInterceptor.class, OriginHandshakeInterceptor.class);
@@ -315,7 +315,7 @@ public class MessageBrokerBeanDefinitionParserTests {
"sockJsScheduler\\[pool size = \\d, active threads = \\d, queued tasks = \\d, " +
"completed tasks = \\d]";
assertThat(actual.matches(expected)).as("\nExpected: " + expected.replace("\\", "") + "\n Actual: " + actual).isTrue();
assertThat(actual).as("\nExpected: " + expected.replace("\\", "") + "\n Actual: " + actual).matches(expected);
}
@Test
@@ -328,7 +328,7 @@ public class MessageBrokerBeanDefinitionParserTests {
assertThat(annotationMethodMessageHandler).isNotNull();
MessageConverter messageConverter = annotationMethodMessageHandler.getMessageConverter();
assertThat(messageConverter).isNotNull();
assertThat(messageConverter instanceof CompositeMessageConverter).isTrue();
assertThat(messageConverter).isInstanceOf(CompositeMessageConverter.class);
String name = MessageBrokerBeanDefinitionParser.MESSAGE_CONVERTER_BEAN_NAME;
CompositeMessageConverter compositeMessageConverter = this.appContext.getBean(name, CompositeMessageConverter.class);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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,14 +70,14 @@ public class ConcurrentWebSocketSessionDecoratorTests {
sendBlockingMessage(decorator);
Thread.sleep(50);
assertThat(decorator.getTimeSinceSendStarted() > 0).isTrue();
assertThat(decorator.getTimeSinceSendStarted()).isGreaterThan(0);
TextMessage payload = new TextMessage("payload");
for (int i = 0; i < 5; i++) {
decorator.sendMessage(payload);
}
assertThat(decorator.getTimeSinceSendStarted() > 0).isTrue();
assertThat(decorator.getTimeSinceSendStarted()).isGreaterThan(0);
assertThat(decorator.getBufferSize()).isEqualTo((5 * payload.getPayloadLength()));
assertThat(session.isOpen()).isTrue();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 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.
@@ -139,7 +139,7 @@ public class OrderedMessageSendingIntegrationTests {
latch.await(5, TimeUnit.SECONDS);
assertThat(concurrentSessionDecorator.getTimeSinceSendStarted() > 0).isTrue();
assertThat(concurrentSessionDecorator.getTimeSinceSendStarted()).isGreaterThan(0);
assertThat(concurrentSessionDecorator.getBufferSize()).isEqualTo((messageCount * MESSAGE_SIZE));
assertThat(handler.getSavedMessages()).containsExactlyElementsOf(expectedMessages);
assertThat(blockingSession.isOpen()).isTrue();

View File

@@ -267,8 +267,8 @@ class StompSubProtocolHandlerTests {
assertThat(this.session.getSentMessages()).hasSize(1);
WebSocketMessage<?> textMessage = this.session.getSentMessages().get(0);
assertThat(((String) textMessage.getPayload()).contains("destination:/user/queue/foo\n")).isTrue();
assertThat(((String) textMessage.getPayload()).contains(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION)).isFalse();
assertThat(((String) textMessage.getPayload())).contains("destination:/user/queue/foo\n");
assertThat(((String) textMessage.getPayload())).doesNotContain(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION);
}
// SPR-12475
@@ -289,7 +289,7 @@ class StompSubProtocolHandlerTests {
assertThat(this.session.getSentMessages()).hasSize(1);
WebSocketMessage<?> webSocketMessage = this.session.getSentMessages().get(0);
assertThat(webSocketMessage instanceof BinaryMessage).isTrue();
assertThat(webSocketMessage).isInstanceOf(BinaryMessage.class);
// Empty payload
@@ -299,7 +299,7 @@ class StompSubProtocolHandlerTests {
assertThat(this.session.getSentMessages()).hasSize(2);
webSocketMessage = this.session.getSentMessages().get(1);
assertThat(webSocketMessage instanceof TextMessage).isTrue();
assertThat(webSocketMessage).isInstanceOf(TextMessage.class);
}
@Test
@@ -417,7 +417,7 @@ class StompSubProtocolHandlerTests {
verifyNoInteractions(this.channel);
assertThat(this.session.getSentMessages()).hasSize(1);
TextMessage actual = (TextMessage) this.session.getSentMessages().get(0);
assertThat(actual.getPayload().startsWith("ERROR")).isTrue();
assertThat(actual.getPayload()).startsWith("ERROR");
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -119,7 +119,7 @@ class StompWebSocketIntegrationTests extends AbstractWebSocketIntegrationTests {
assertThat(clientHandler.latch.await(TIMEOUT, TimeUnit.SECONDS)).isTrue();
String payload = clientHandler.actual.get(1).getPayload();
assertThat(payload.startsWith("MESSAGE\n")).as("Expected STOMP MESSAGE, got " + payload).isTrue();
assertThat(payload).as("Expected STOMP MESSAGE, got " + payload).startsWith("MESSAGE\n");
}
}
@@ -137,8 +137,8 @@ class StompWebSocketIntegrationTests extends AbstractWebSocketIntegrationTests {
assertThat(session).isNotNull();
assertThat(clientHandler.latch.await(TIMEOUT, TimeUnit.SECONDS)).isTrue();
String payload = clientHandler.actual.get(1).getPayload();
assertThat(payload.contains(destHeader)).as("Expected STOMP destination=/app/number, got " + payload).isTrue();
assertThat(payload.contains("42")).as("Expected STOMP Payload=42, got " + payload).isTrue();
assertThat(payload).as("Expected STOMP destination=/app/number, got " + payload).contains(destHeader);
assertThat(payload).as("Expected STOMP Payload=42, got " + payload).contains("42");
}
}
@@ -157,9 +157,9 @@ class StompWebSocketIntegrationTests extends AbstractWebSocketIntegrationTests {
assertThat(session).isNotNull();
assertThat(clientHandler.latch.await(TIMEOUT, TimeUnit.SECONDS)).isTrue();
String payload = clientHandler.actual.get(1).getPayload();
assertThat(payload.startsWith("MESSAGE\n")).isTrue();
assertThat(payload.contains("destination:/user/queue/error\n")).isTrue();
assertThat(payload.endsWith("Got error: Bad input\0")).isTrue();
assertThat(payload).startsWith("MESSAGE\n");
assertThat(payload).contains("destination:/user/queue/error\n");
assertThat(payload).endsWith("Got error: Bad input\0");
}
}
@@ -179,9 +179,9 @@ class StompWebSocketIntegrationTests extends AbstractWebSocketIntegrationTests {
assertThat(session).isNotNull();
assertThat(clientHandler.latch.await(TIMEOUT, TimeUnit.SECONDS)).isTrue();
String payload = clientHandler.actual.get(1).getPayload();
assertThat(payload.startsWith("MESSAGE\n")).isTrue();
assertThat(payload.contains("destination:/topic/scopedBeanValue\n")).isTrue();
assertThat(payload.endsWith("55\0")).isTrue();
assertThat(payload).startsWith("MESSAGE\n");
assertThat(payload).contains("destination:/topic/scopedBeanValue\n");
assertThat(payload).endsWith("55\0");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -275,7 +275,7 @@ abstract class AbstractSockJsIntegrationTests {
for (TextMessage message : messages) {
assertThat(handler.receivedMessages.remove(message)).as("Message not received: " + message).isTrue();
}
assertThat(handler.receivedMessages.size()).as("Remaining messages: " + handler.receivedMessages).isEqualTo(0);
assertThat(handler.receivedMessages).as("Remaining messages: " + handler.receivedMessages).isEmpty();
session.close();
}

View File

@@ -88,7 +88,7 @@ class SockJsClientTests {
this.sockJsClient.doHandshake(handler, URL);
assertThat(this.webSocketTransport.invoked()).isFalse();
assertThat(this.xhrTransport.invoked()).isTrue();
assertThat(this.xhrTransport.getRequest().getTransportUrl().toString().endsWith("xhr_streaming")).isTrue();
assertThat(this.xhrTransport.getRequest().getTransportUrl().toString()).endsWith("xhr_streaming");
}
@Test
@@ -99,7 +99,7 @@ class SockJsClientTests {
this.sockJsClient.doHandshake(handler, URL).addCallback(this.connectCallback);
assertThat(this.webSocketTransport.invoked()).isFalse();
assertThat(this.xhrTransport.invoked()).isTrue();
assertThat(this.xhrTransport.getRequest().getTransportUrl().toString().endsWith("xhr")).isTrue();
assertThat(this.xhrTransport.getRequest().getTransportUrl().toString()).endsWith("xhr");
}
@Test // SPR-13254

View File

@@ -57,19 +57,19 @@ class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSession> {
Thread.sleep(1);
long time1 = this.session.getTimeSinceLastActive();
assertThat(time1 > 0).isTrue();
assertThat(time1).isGreaterThan(0);
Thread.sleep(1);
long time2 = this.session.getTimeSinceLastActive();
assertThat(time2 > time1).isTrue();
assertThat(time2).isGreaterThan(time1);
this.session.delegateConnectionEstablished();
Thread.sleep(1);
this.session.setActive(false);
assertThat(this.session.getTimeSinceLastActive() > 0).isTrue();
assertThat(this.session.getTimeSinceLastActive()).isGreaterThan(0);
this.session.setActive(true);
assertThat(this.session.getTimeSinceLastActive()).isEqualTo(0);