Mockito Polishing

`$ find spring-integration-* | grep '\.java$' | xargs sed -E -i '' -e 's/(\(?)\(([^<()]*)\) *invocation.getArguments\(\)\[([0-9]*)\]/\1invocation.getArgumentAt(\3, \2.class)/'`
This commit is contained in:
Gary Russell
2016-10-28 08:16:56 -04:00
committed by Artem Bilan
parent 9225c514fe
commit c5fbd93787
27 changed files with 58 additions and 58 deletions

View File

@@ -128,7 +128,7 @@ public class DispatcherHasNoSubscribersTests {
Log logger = mock(Log.class);
final ArrayList<String> logList = new ArrayList<String>();
doAnswer(invocation -> {
String message = (String) invocation.getArguments()[0];
String message = invocation.getArgumentAt(0, String.class);
if (message.startsWith("Dispatcher has no subscribers")) {
logList.add(message);
}

View File

@@ -167,7 +167,7 @@ public class InboundEndpointTests {
Mockito.doAnswer(invocation -> {
org.springframework.amqp.core.Message message =
(org.springframework.amqp.core.Message) invocation.getArguments()[2];
invocation.getArgumentAt(2, org.springframework.amqp.core.Message.class);
Map<String, Object> headers = message.getMessageProperties().getHeaders();
assertTrue(headers.containsKey(JsonHeaders.TYPE_ID.replaceFirst(JsonHeaders.PREFIX, "")));
assertNotEquals("foo", headers.get(JsonHeaders.TYPE_ID.replaceFirst(JsonHeaders.PREFIX, "")));

View File

@@ -120,7 +120,7 @@ public class OutboundEndpointTests {
final AtomicReference<Message> amqpMessage =
new AtomicReference<Message>();
willAnswer(invocation -> {
amqpMessage.set((Message) invocation.getArguments()[2]);
amqpMessage.set(invocation.getArgumentAt(2, Message.class));
return null;
}).given(amqpTemplate).send(anyString(), anyString(), any(Message.class),
any(CorrelationData.class));
@@ -144,7 +144,7 @@ public class OutboundEndpointTests {
final AtomicReference<Message> amqpMessage =
new AtomicReference<Message>();
willAnswer(invocation -> {
amqpMessage.set((Message) invocation.getArguments()[2]);
amqpMessage.set(invocation.getArgumentAt(2, Message.class));
return null;
}).given(amqpTemplate)
.doSendAndReceiveWithTemporary(anyString(), anyString(), any(Message.class), any(CorrelationData.class));

View File

@@ -75,7 +75,7 @@ public class P2pChannelTests {
when(logger.isInfoEnabled()).thenReturn(true);
final List<String> logs = new ArrayList<String>();
doAnswer(invocation -> {
logs.add((String) invocation.getArguments()[0]);
logs.add(invocation.getArgumentAt(0, String.class));
return null;
}).when(logger).info(Mockito.anyString());
ReflectionUtils.doWithFields(AbstractMessageChannel.class, field -> {

View File

@@ -309,7 +309,7 @@ public class ChainParserTests {
final AtomicReference<String> log = new AtomicReference<String>();
when(logger.isWarnEnabled()).thenReturn(true);
doAnswer(invocation -> {
log.set((String) invocation.getArguments()[0]);
log.set(invocation.getArgumentAt(0, String.class));
return null;
}).when(logger).warn(any());

View File

@@ -276,7 +276,7 @@ public class BroadcastingDispatcherTests {
private void defaultTaskExecutorMock() {
Mockito.doAnswer(invocation -> {
((Runnable) invocation.getArguments()[0]).run();
(invocation.getArgumentAt(0, Runnable.class)).run();
return null;
}).when(taskExecutorMock).execute(Mockito.any(Runnable.class));
}
@@ -288,7 +288,7 @@ public class BroadcastingDispatcherTests {
final AtomicInteger count = new AtomicInteger();
Mockito.doAnswer(invocation -> {
if (passes[count.getAndIncrement()]) {
((Runnable) invocation.getArguments()[0]).run();
(invocation.getArgumentAt(0, Runnable.class)).run();
}
return null;
}).when(taskExecutorMock).execute(Mockito.any(Runnable.class));

View File

@@ -111,8 +111,8 @@ public class AsyncHandlerTests {
Log logger = spy(TestUtils.getPropertyValue(this.handler, "logger", Log.class));
new DirectFieldAccessor(this.handler).setPropertyValue("logger", logger);
doAnswer(invocation -> {
failedCallbackMessage = (String) invocation.getArguments()[0];
failedCallbackException = (Exception) invocation.getArguments()[1];
failedCallbackMessage = invocation.getArgumentAt(0, String.class);
failedCallbackException = invocation.getArgumentAt(1, Exception.class);
exceptionLatch.countDown();
return null;
}).when(logger).error(anyString(), any(Throwable.class));

View File

@@ -897,7 +897,7 @@ public class AdvisedMessageHandlerTests {
when(logger.isWarnEnabled()).thenReturn(Boolean.TRUE);
final AtomicReference<String> logMessage = new AtomicReference<String>();
doAnswer(invocation -> {
logMessage.set((String) invocation.getArguments()[0]);
logMessage.set(invocation.getArgumentAt(0, String.class));
return null;
}).when(logger).warn(Mockito.anyString());
DirectFieldAccessor accessor = new DirectFieldAccessor(advice);

View File

@@ -309,7 +309,7 @@ public class RemoteFileOutboundGatewayTests {
}).when(session).rename(anyString(), anyString());
final List<String> madeDirs = new ArrayList<String>();
doAnswer(invocation -> {
madeDirs.add((String) invocation.getArguments()[0]);
madeDirs.add(invocation.getArgumentAt(0, String.class));
return null;
}).when(session).mkdir(anyString());
when(sessionFactory.getSession()).thenReturn(session);
@@ -909,7 +909,7 @@ public class RemoteFileOutboundGatewayTests {
when(sessionFactory.getSession()).thenReturn(session);
final AtomicReference<String> written = new AtomicReference<String>();
doAnswer(invocation -> {
written.set((String) invocation.getArguments()[1]);
written.set(invocation.getArgumentAt(1, String.class));
return null;
}).when(session).write(any(InputStream.class), anyString());
tempFolder.newFile("baz.txt");
@@ -943,7 +943,7 @@ public class RemoteFileOutboundGatewayTests {
when(sessionFactory.getSession()).thenReturn(session);
final AtomicReference<String> written = new AtomicReference<String>();
doAnswer(invocation -> {
written.set((String) invocation.getArguments()[1]);
written.set(invocation.getArgumentAt(1, String.class));
return null;
}).when(session).write(any(InputStream.class), anyString());
tempFolder.newFile("baz.txt");

View File

@@ -64,7 +64,7 @@ public class FileTransferringMessageHandlerTests {
when(sf.getSession()).thenReturn(session);
doAnswer(invocation -> {
String path = (String) invocation.getArguments()[1];
String path = invocation.getArgumentAt(1, String.class);
assertFalse(path.startsWith("/"));
return null;
}).when(session).rename(Mockito.anyString(), Mockito.anyString());
@@ -87,8 +87,8 @@ public class FileTransferringMessageHandlerTests {
final AtomicReference<String> finalPath = new AtomicReference<String>();
when(sf.getSession()).thenReturn(session);
doAnswer(invocation -> {
temporaryPath.set((String) invocation.getArguments()[0]);
finalPath.set((String) invocation.getArguments()[1]);
temporaryPath.set(invocation.getArgumentAt(0, String.class));
finalPath.set(invocation.getArgumentAt(1, String.class));
return null;
}).when(session).rename(Mockito.anyString(), Mockito.anyString());
FileTransferringMessageHandler<F> handler = new FileTransferringMessageHandler<F>(sf);
@@ -110,7 +110,7 @@ public class FileTransferringMessageHandlerTests {
when(sf.getSession()).thenReturn(session);
doAnswer(invocation -> {
String path = (String) invocation.getArguments()[1];
String path = invocation.getArgumentAt(1, String.class);
assertFalse(path.startsWith("/"));
return null;
}).when(session).rename(Mockito.anyString(), Mockito.anyString());

View File

@@ -161,7 +161,7 @@ public class FtpOutboundTests {
when(logger.isWarnEnabled()).thenReturn(true);
final AtomicReference<String> logged = new AtomicReference<String>();
doAnswer(invocation -> {
logged.set((String) invocation.getArguments()[0]);
logged.set(invocation.getArgumentAt(0, String.class));
invocation.callRealMethod();
return null;
}).when(logger).warn(Mockito.anyString());
@@ -231,8 +231,8 @@ public class FtpOutboundTests {
when(ftpClient.storeFile(Mockito.anyString(), any(InputStream.class))).thenAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
String fileName = (String) invocation.getArguments()[0];
InputStream fis = (InputStream) invocation.getArguments()[1];
String fileName = invocation.getArgumentAt(0, String.class);
InputStream fis = invocation.getArgumentAt(1, InputStream.class);
FileCopyUtils.copy(fis, new FileOutputStream(fileName));
return true;
}
@@ -241,8 +241,8 @@ public class FtpOutboundTests {
@Override
public Boolean answer(InvocationOnMock invocation)
throws Throwable {
File file = new File((String) invocation.getArguments()[0]);
File renameToFile = new File((String) invocation.getArguments()[1]);
File file = new File(invocation.getArgumentAt(0, String.class));
File renameToFile = new File(invocation.getArgumentAt(1, String.class));
file.renameTo(renameToFile);
return true;
}

View File

@@ -120,7 +120,7 @@ public class HttpProxyScenarioTests {
final String contentDispositionValue = "attachment; filename=\"test.txt\"";
Mockito.doAnswer(invocation -> {
URI uri = (URI) invocation.getArguments()[0];
URI uri = invocation.getArgumentAt(0, URI.class);
assertEquals(new URI("http://testServer/test?foo=bar&FOO=BAR"), uri);
HttpEntity<?> httpEntity = (HttpEntity<?>) invocation.getArguments()[2];
HttpHeaders httpHeaders = httpEntity.getHeaders();
@@ -173,7 +173,7 @@ public class HttpProxyScenarioTests {
RestTemplate template = Mockito.spy(new RestTemplate());
Mockito.doAnswer(invocation -> {
URI uri = (URI) invocation.getArguments()[0];
URI uri = invocation.getArgumentAt(0, URI.class);
assertEquals(new URI("http://testServer/testmp"), uri);
HttpEntity<?> httpEntity = (HttpEntity<?>) invocation.getArguments()[2];
HttpHeaders httpHeaders = httpEntity.getHeaders();

View File

@@ -73,7 +73,7 @@ public class MultipartAsRawByteArrayTests {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
byte[] buff = (byte[]) invocation.getArguments()[0];
byte[] buff = invocation.getArgumentAt(0, byte[].class);
buff[0] = 'f';
buff[1] = 'o';
buff[2] = 'o';

View File

@@ -776,7 +776,7 @@ public class CachingClientConnectionFactoryTests {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
invocation.callRealMethod();
String log = (String) invocation.getArguments()[0];
String log = invocation.getArgumentAt(0, String.class);
if (log.startsWith("Response")) {
Executors.newSingleThreadScheduledExecutor().execute(new Runnable() {

View File

@@ -303,7 +303,7 @@ public class TcpNioConnectionTests {
Socket socket = mock(Socket.class);
Mockito.when(channel.socket()).thenReturn(socket);
doAnswer(invocation -> {
ByteBuffer buffer = (ByteBuffer) invocation.getArguments()[0];
ByteBuffer buffer = invocation.getArgumentAt(0, ByteBuffer.class);
buffer.position(1);
return 1;
}).when(channel).read(Mockito.any(ByteBuffer.class));
@@ -343,7 +343,7 @@ public class TcpNioConnectionTests {
Socket socket = mock(Socket.class);
Mockito.when(channel.socket()).thenReturn(socket);
doAnswer(invocation -> {
ByteBuffer buffer = (ByteBuffer) invocation.getArguments()[0];
ByteBuffer buffer = invocation.getArgumentAt(0, ByteBuffer.class);
buffer.position(1025);
buffer.put((byte) '\r');
buffer.put((byte) '\n');
@@ -501,7 +501,7 @@ public class TcpNioConnectionTests {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
ByteBuffer buff = (ByteBuffer) invocation.getArguments()[0];
ByteBuffer buff = invocation.getArgumentAt(0, ByteBuffer.class);
byte[] bytes = written.toByteArray();
buff.put(bytes);
return bytes.length;
@@ -516,7 +516,7 @@ public class TcpNioConnectionTests {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
ByteBuffer buff = (ByteBuffer) invocation.getArguments()[0];
ByteBuffer buff = invocation.getArgumentAt(0, ByteBuffer.class);
byte[] bytes = new byte[buff.limit()];
buff.get(bytes);
written.write(bytes);

View File

@@ -533,7 +533,7 @@ public class DefaultJmsHeaderMapperTests {
Session session = Mockito.mock(Session.class);
Mockito.doAnswer(invocation -> new StubTextMessage((String) invocation.getArguments()[0])).when(session)
Mockito.doAnswer(invocation -> new StubTextMessage(invocation.getArgumentAt(0, String.class))).when(session)
.createTextMessage(Mockito.anyString());
javax.jms.Message request = converter.toMessage(new Foo(), session);

View File

@@ -318,7 +318,7 @@ public class SubscribableJmsChannelTests {
Log logger = mock(Log.class);
final ArrayList<String> logList = new ArrayList<String>();
doAnswer(invocation -> {
String message = (String) invocation.getArguments()[0];
String message = invocation.getArgumentAt(0, String.class);
if (message.startsWith("Dispatcher has no subscribers")) {
logList.add(message);
}

View File

@@ -85,7 +85,7 @@ public class MessageWithContentTypeTests {
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
MimeMessage mimeMessage = (MimeMessage) invocation.getArguments()[0];
MimeMessage mimeMessage = invocation.getArgumentAt(0, MimeMessage.class);
assertEquals("text/html", mimeMessage.getDataHandler().getContentType());
return null;
}

View File

@@ -84,7 +84,7 @@ public class DownstreamExceptionTests {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
if (((String) invocation.getArguments()[0]).contains("Unhandled")) {
if ((invocation.getArgumentAt(0, String.class)).contains("Unhandled")) {
latch.countDown();
}
return null;

View File

@@ -167,7 +167,7 @@ public class MqttAdapterTests {
@Override
public MqttToken answer(InvocationOnMock invocation) throws Throwable {
MqttConnectOptions options = (MqttConnectOptions) invocation.getArguments()[0];
MqttConnectOptions options = invocation.getArgumentAt(0, MqttConnectOptions.class);
assertEquals(23, options.getConnectionTimeout());
assertEquals(45, options.getKeepAliveInterval());
assertEquals("pass", new String(options.getPassword()));
@@ -190,7 +190,7 @@ public class MqttAdapterTests {
@Override
public MqttDeliveryToken answer(InvocationOnMock invocation) throws Throwable {
assertEquals("mqtt-foo", invocation.getArguments()[0]);
MqttMessage message = (MqttMessage) invocation.getArguments()[1];
MqttMessage message = invocation.getArgumentAt(1, MqttMessage.class);
assertEquals("Hello, world!", new String(message.getPayload()));
publishCalled.set(true);
return deliveryToken;
@@ -246,7 +246,7 @@ public class MqttAdapterTests {
waitToFail.await(10, TimeUnit.SECONDS);
throw reconnectException;
}
MqttConnectOptions options = (MqttConnectOptions) invocation.getArguments()[0];
MqttConnectOptions options = invocation.getArgumentAt(0, MqttConnectOptions.class);
assertEquals(23, options.getConnectionTimeout());
assertEquals(45, options.getKeepAliveInterval());
assertEquals("pass", new String(options.getPassword()));
@@ -269,7 +269,7 @@ public class MqttAdapterTests {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
callback.set((MqttCallback) invocation.getArguments()[0]);
callback.set(invocation.getArgumentAt(0, MqttCallback.class));
return null;
}
}).when(client).setCallback(any(MqttCallback.class));
@@ -290,7 +290,7 @@ public class MqttAdapterTests {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
events.add((MqttIntegrationEvent) invocation.getArguments()[0]);
events.add(invocation.getArgumentAt(0, MqttIntegrationEvent.class));
return null;
}
}).when(applicationEventPublisher).publishEvent(any(MqttIntegrationEvent.class));

View File

@@ -209,7 +209,7 @@ public class SftpOutboundTests {
handler.afterPropertiesSet();
final List<String> madeDirs = new ArrayList<String>();
doAnswer(invocation -> {
madeDirs.add((String) invocation.getArguments()[0]);
madeDirs.add(invocation.getArgumentAt(0, String.class));
return null;
}).when(session).mkdir(anyString());
handler.handleMessage(new GenericMessage<String>("qux"));
@@ -356,16 +356,16 @@ public class SftpOutboundTests {
ChannelSftp channel = mock(ChannelSftp.class);
doAnswer(invocation -> {
File file = new File((String) invocation.getArguments()[1]);
File file = new File(invocation.getArgumentAt(1, String.class));
assertTrue(file.getName().endsWith(".writing"));
FileCopyUtils.copy((InputStream) invocation.getArguments()[0], new FileOutputStream(file));
FileCopyUtils.copy(invocation.getArgumentAt(0, InputStream.class), new FileOutputStream(file));
return null;
}).when(channel).put(Mockito.any(InputStream.class), Mockito.anyString());
doAnswer(invocation -> {
File file = new File((String) invocation.getArguments()[0]);
File file = new File(invocation.getArgumentAt(0, String.class));
assertTrue(file.getName().endsWith(".writing"));
File renameToFile = new File((String) invocation.getArguments()[1]);
File renameToFile = new File(invocation.getArgumentAt(1, String.class));
file.renameTo(renameToFile);
return null;
}).when(channel).rename(Mockito.anyString(), Mockito.anyString());

View File

@@ -118,7 +118,7 @@ public class SyslogReceivingChannelAdapterTests {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
if (((String) invocation.getArguments()[0]).contains("Error on syslog socket")) {
if ((invocation.getArgumentAt(0, String.class)).contains("Error on syslog socket")) {
sawLog.countDown();
}
invocation.callRealMethod();
@@ -201,7 +201,7 @@ public class SyslogReceivingChannelAdapterTests {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
if (((String) invocation.getArguments()[0]).contains("Error on syslog socket")) {
if ((invocation.getArgumentAt(0, String.class)).contains("Error on syslog socket")) {
sawLog.countDown();
}
invocation.callRealMethod();

View File

@@ -84,7 +84,7 @@ public class TwitterSearchOutboundGatewayTests {
@Override
public SearchResults answer(InvocationOnMock invocation) throws Throwable {
SearchParameters searchParameters = (SearchParameters) invocation.getArguments()[0];
SearchParameters searchParameters = invocation.getArgumentAt(0, SearchParameters.class);
assertEquals("foo", searchParameters.getQuery());
assertEquals(Integer.valueOf(20), searchParameters.getCount());
return searchResults;
@@ -111,7 +111,7 @@ public class TwitterSearchOutboundGatewayTests {
@Override
public SearchResults answer(InvocationOnMock invocation) throws Throwable {
SearchParameters searchParameters = (SearchParameters) invocation.getArguments()[0];
SearchParameters searchParameters = invocation.getArgumentAt(0, SearchParameters.class);
assertEquals("foo", searchParameters.getQuery());
assertEquals(Integer.valueOf(30), searchParameters.getCount());
return searchResults;
@@ -138,7 +138,7 @@ public class TwitterSearchOutboundGatewayTests {
@Override
public SearchResults answer(InvocationOnMock invocation) throws Throwable {
SearchParameters searchParameters = (SearchParameters) invocation.getArguments()[0];
SearchParameters searchParameters = invocation.getArgumentAt(0, SearchParameters.class);
assertEquals("bar", searchParameters.getQuery());
assertEquals(Integer.valueOf(1), searchParameters.getCount());
assertEquals(Long.valueOf(2), searchParameters.getSinceId());
@@ -166,7 +166,7 @@ public class TwitterSearchOutboundGatewayTests {
@Override
public SearchResults answer(InvocationOnMock invocation) throws Throwable {
SearchParameters searchParameters = (SearchParameters) invocation.getArguments()[0];
SearchParameters searchParameters = invocation.getArgumentAt(0, SearchParameters.class);
assertSame(parameters, searchParameters);
return searchResults;
}
@@ -192,7 +192,7 @@ public class TwitterSearchOutboundGatewayTests {
@Override
public SearchResults answer(InvocationOnMock invocation) throws Throwable {
SearchParameters searchParameters = (SearchParameters) invocation.getArguments()[0];
SearchParameters searchParameters = invocation.getArgumentAt(0, SearchParameters.class);
assertEquals("foobar", searchParameters.getQuery());
assertEquals(Integer.valueOf(5), searchParameters.getCount());
assertEquals(Long.valueOf(11), searchParameters.getSinceId());
@@ -218,7 +218,7 @@ public class TwitterSearchOutboundGatewayTests {
@Override
public SearchResults answer(InvocationOnMock invocation) throws Throwable {
SearchParameters searchParameters = (SearchParameters) invocation.getArguments()[0];
SearchParameters searchParameters = invocation.getArgumentAt(0, SearchParameters.class);
assertEquals("foo", searchParameters.getQuery());
assertEquals(Integer.valueOf(20), searchParameters.getCount());
return searchResults;

View File

@@ -119,7 +119,7 @@ public class UriVariableTests {
Mockito.doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
uri.set((String) invocation.getArguments()[0]);
uri.set(invocation.getArgumentAt(0, String.class));
throw new WebServiceIOException("intentional");
}
}).when(webServiceTemplate)

View File

@@ -61,7 +61,7 @@ public class XmppHeaderEnricherParserTests {
MessageHandler handler = mock(MessageHandler.class);
doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) throws Throwable {
Message message = (Message) invocation.getArguments()[0];
Message message = invocation.getArgumentAt(0, Message.class);
String chatToUser = (String) message.getHeaders().get(XmppHeaders.TO);
assertNotNull(chatToUser);
assertEquals("test1@example.org", chatToUser);

View File

@@ -85,7 +85,7 @@ public class ChatMessageListeningEndpointTests {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
packetListSet.add((StanzaListener) invocation.getArguments()[0]);
packetListSet.add(invocation.getArgumentAt(0, StanzaListener.class));
return null;
}

View File

@@ -70,7 +70,7 @@ public class PresenceListeningEndpointTests {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
rosterSet.add((RosterListener) invocation.getArguments()[0]);
rosterSet.add(invocation.getArgumentAt(0, RosterListener.class));
return null;
}
@@ -80,7 +80,7 @@ public class PresenceListeningEndpointTests {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
rosterSet.remove((RosterListener) invocation.getArguments()[0]);
rosterSet.remove(invocation.getArgumentAt(0, RosterListener.class));
return null;
}