Skip to content

Commit

Permalink
Upgrade to Mockito-5.3.0
Browse files Browse the repository at this point in the history
Fix `ImapMailReceiverTests` and `Pop3MailReceiverTests` to not use mocks for messages
  • Loading branch information
artembilan committed Apr 20, 2023
1 parent 3f27dfb commit 5e34809
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 65 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ ext {
mailVersion = '1.0.0'
micrometerTracingVersion = '1.1.0-RC1'
micrometerVersion = '1.11.0-RC1'
mockitoVersion = '5.2.0'
mockitoVersion = '5.3.0'
mongoDriverVersion = '4.9.1'
mysqlVersion = '8.0.32'
pahoMqttClientVersion = '1.2.5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
@SpringJUnitConfig
@ContextConfiguration(
"classpath:org/springframework/integration/mail/config/ImapIdleChannelAdapterParserTests-context.xml")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext
public class ImapMailReceiverTests {

private AtomicInteger failed;
Expand Down Expand Up @@ -269,11 +269,11 @@ public void receiveAndMarkAsReadDontDelete() throws Exception {
user.deliver(GreenMailUtil.createTextEmail("user", "sender", "subject", "body",
imapIdleServer.getImap().getServerSetup()));
AbstractMailReceiver receiver = new ImapMailReceiver();
Message msg1 = spy(GreenMailUtil.newMimeMessage("test1"));
Message msg2 = spy(GreenMailUtil.newMimeMessage("test2"));
Message msg1 = GreenMailUtil.newMimeMessage("test1");
Message msg2 = GreenMailUtil.newMimeMessage("test2");
receiver = receiveAndMarkAsReadDontDeleteGuts(receiver, msg1, msg2);
verify(msg1, times(1)).setFlag(Flag.SEEN, true);
verify(msg2, times(1)).setFlag(Flag.SEEN, true);
assertThat(msg1.getFlags().contains(Flag.SEEN)).isTrue();
assertThat(msg2.getFlags().contains(Flag.SEEN)).isTrue();
verify(receiver, times(0)).deleteMessages(Mockito.any());
}

Expand Down Expand Up @@ -312,28 +312,28 @@ private AbstractMailReceiver receiveAndMarkAsReadDontDeleteGuts(AbstractMailRece
@Test
public void receiveAndMarkAsReadDontDeletePassingFilter() throws Exception {
AbstractMailReceiver receiver = new ImapMailReceiver();
Message msg1 = spy(GreenMailUtil.newMimeMessage("test1"));
Message msg2 = spy(GreenMailUtil.newMimeMessage("test2"));
Message msg1 = GreenMailUtil.newMimeMessage("test1");
Message msg2 = GreenMailUtil.newMimeMessage("test2");
Expression selectorExpression = new SpelExpressionParser().parseExpression("true");
receiver.setSelectorExpression(selectorExpression);
receiver = receiveAndMarkAsReadDontDeleteGuts(receiver, msg1, msg2);
verify(msg1, times(1)).setFlag(Flag.SEEN, true);
verify(msg2, times(1)).setFlag(Flag.SEEN, true);
assertThat(msg1.getFlags().contains(Flag.SEEN)).isTrue();
assertThat(msg2.getFlags().contains(Flag.SEEN)).isTrue();
verify(receiver, times(0)).deleteMessages(Mockito.any());
}

@Test
public void receiveAndMarkAsReadDontDeleteFiltered() throws Exception {
AbstractMailReceiver receiver = new ImapMailReceiver();
Message msg1 = spy(GreenMailUtil.newMimeMessage("test1"));
Message msg2 = spy(GreenMailUtil.newMimeMessage("test2"));
Message msg1 = GreenMailUtil.newMimeMessage("test1");
Message msg2 = GreenMailUtil.newMimeMessage("test2");
given(msg2.getSubject()).willReturn("foo"); // should not be marked seen
Expression selectorExpression = new SpelExpressionParser()
.parseExpression("subject == null OR !subject.equals('foo')");
receiver.setSelectorExpression(selectorExpression);
receiver = receiveAndMarkAsReadDontDeleteGuts(receiver, msg1, msg2);
verify(msg1, times(1)).setFlag(Flag.SEEN, true);
verify(msg2, never()).setFlag(Flag.SEEN, true);
assertThat(msg1.getFlags().contains(Flag.SEEN)).isTrue();
assertThat(msg2.getFlags().contains(Flag.SEEN)).isTrue();
verify(receiver, times(0)).deleteMessages(Mockito.any());
}

Expand Down Expand Up @@ -400,8 +400,8 @@ public void receiveMarkAsReadAndDelete() throws Exception {
given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
folderField.set(receiver, folder);

Message msg1 = spy(GreenMailUtil.newMimeMessage("test1"));
Message msg2 = spy(GreenMailUtil.newMimeMessage("test2"));
Message msg1 = GreenMailUtil.newMimeMessage("test1");
Message msg2 = GreenMailUtil.newMimeMessage("test2");
final Message[] messages = new Message[] {msg1, msg2};
willAnswer(invocation -> {
DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
Expand All @@ -416,8 +416,10 @@ public void receiveMarkAsReadAndDelete() throws Exception {

willAnswer(invocation -> null).given(receiver).fetchMessages(messages);
receiver.receive();
verify(msg1, times(1)).setFlag(Flag.SEEN, true);
verify(msg2, times(1)).setFlag(Flag.SEEN, true);

assertThat(msg1.getFlags().contains(Flag.SEEN)).isTrue();
assertThat(msg2.getFlags().contains(Flag.SEEN)).isTrue();

verify(receiver, times(1)).deleteMessages(Mockito.any());
}

Expand All @@ -436,8 +438,8 @@ public void receiveAndDontMarkAsRead() throws Exception {
folderField.set(receiver, folder);


Message msg1 = spy(GreenMailUtil.newMimeMessage("test1"));
Message msg2 = spy(GreenMailUtil.newMimeMessage("test2"));
Message msg1 = GreenMailUtil.newMimeMessage("test1");
Message msg2 = GreenMailUtil.newMimeMessage("test2");
final Message[] messages = new Message[] {msg1, msg2};
willAnswer(invocation -> null).given(receiver).openFolder();

Expand All @@ -446,8 +448,8 @@ public void receiveAndDontMarkAsRead() throws Exception {
willAnswer(invocation -> null).given(receiver).fetchMessages(messages);
receiver.afterPropertiesSet();
receiver.receive();
verify(msg1, times(0)).setFlag(Flag.SEEN, true);
verify(msg2, times(0)).setFlag(Flag.SEEN, true);
assertThat(msg1.getFlags().contains(Flag.SEEN)).isFalse();
assertThat(msg2.getFlags().contains(Flag.SEEN)).isFalse();
}

@Test
Expand All @@ -465,8 +467,8 @@ public void receiveAndDontMarkAsReadButDelete() throws Exception {
given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
folderField.set(receiver, folder);

Message msg1 = spy(GreenMailUtil.newMimeMessage("test1"));
Message msg2 = spy(GreenMailUtil.newMimeMessage("test2"));
Message msg1 = GreenMailUtil.newMimeMessage("test1");
Message msg2 = GreenMailUtil.newMimeMessage("test2");
final Message[] messages = new Message[] {msg1, msg2};
willAnswer(invocation -> {
DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
Expand All @@ -482,10 +484,11 @@ public void receiveAndDontMarkAsReadButDelete() throws Exception {
willAnswer(invocation -> null).given(receiver).fetchMessages(messages);
receiver.afterPropertiesSet();
receiver.receive();
verify(msg1, times(0)).setFlag(Flag.SEEN, true);
verify(msg2, times(0)).setFlag(Flag.SEEN, true);
verify(msg1, times(1)).setFlag(Flag.DELETED, true);
verify(msg2, times(1)).setFlag(Flag.DELETED, true);

assertThat(msg1.getFlags().contains(Flag.SEEN)).isFalse();
assertThat(msg2.getFlags().contains(Flag.SEEN)).isFalse();
assertThat(msg1.getFlags().contains(Flag.DELETED)).isTrue();
assertThat(msg2.getFlags().contains(Flag.DELETED)).isTrue();
}

@Test
Expand All @@ -501,8 +504,8 @@ public void receiveAndIgnoreMarkAsReadDontDelete() throws Exception {
given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
folderField.set(receiver, folder);

Message msg1 = spy(GreenMailUtil.newMimeMessage("test1"));
Message msg2 = spy(GreenMailUtil.newMimeMessage("test2"));
Message msg1 = GreenMailUtil.newMimeMessage("test1");
Message msg2 = GreenMailUtil.newMimeMessage("test2");
final Message[] messages = new Message[] {msg1, msg2};
willAnswer(invocation -> {
DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
Expand All @@ -517,8 +520,8 @@ public void receiveAndIgnoreMarkAsReadDontDelete() throws Exception {

willAnswer(invocation -> null).given(receiver).fetchMessages(messages);
receiver.receive();
verify(msg1, times(1)).setFlag(Flag.SEEN, true);
verify(msg2, times(1)).setFlag(Flag.SEEN, true);
assertThat(msg1.getFlags().contains(Flag.SEEN)).isTrue();
assertThat(msg2.getFlags().contains(Flag.SEEN)).isTrue();
verify(receiver, times(0)).deleteMessages(Mockito.any());
}

Expand All @@ -535,9 +538,7 @@ public void testMessageHistory() throws Exception {
DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
adapterAccessor.setPropertyValue("mailReceiver", receiver);

Message mailMessage = spy(GreenMailUtil.newMimeMessage("test1"));
Flags flags = mock(Flags.class);
given(mailMessage.getFlags()).willReturn(flags);
Message mailMessage = GreenMailUtil.newMimeMessage("test1");
final Message[] messages = new Message[] {mailMessage};

willAnswer(invocation -> {
Expand Down Expand Up @@ -603,10 +604,8 @@ protected Object handleRequestMessage(org.springframework.messaging.Message<?> r
DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
adapterAccessor.setPropertyValue("mailReceiver", receiver);

Message mailMessage = spy(GreenMailUtil.newMimeMessage("test1"));
Flags flags = mock(Flags.class);
given(mailMessage.getFlags()).willReturn(flags);
final Message[] messages = new Message[] {mailMessage};
Message mailMessage = GreenMailUtil.newMimeMessage("test1");
Message[] messages = new Message[] {mailMessage};

willAnswer(invocation -> messages).given(receiver).searchForNewMessages();

Expand Down Expand Up @@ -650,9 +649,7 @@ public void testNoInitialIdleDelayWhenRecentNotSupported() throws Exception {

willAnswer(invocation -> folder).given(receiver).getFolder();

Message mailMessage = spy(GreenMailUtil.newMimeMessage("test1"));
Flags flags = mock(Flags.class);
given(mailMessage.getFlags()).willReturn(flags);
Message mailMessage = GreenMailUtil.newMimeMessage("test1");
final Message[] messages = new Message[] {mailMessage};

final AtomicInteger shouldFindMessagesCounter = new AtomicInteger(2);
Expand Down Expand Up @@ -724,9 +721,7 @@ public void testInitialIdleDelayWhenRecentIsSupported() throws Exception {

willAnswer(invocation -> folder).given(receiver).getFolder();

Message mailMessage = spy(GreenMailUtil.newMimeMessage("test1"));
Flags flags = mock(Flags.class);
given(mailMessage.getFlags()).willReturn(flags);
Message mailMessage = GreenMailUtil.newMimeMessage("test1");
final Message[] messages = new Message[] {mailMessage};

willAnswer(invocation -> messages).given(receiver).searchForNewMessages();
Expand All @@ -747,7 +742,7 @@ public void testInitialIdleDelayWhenRecentIsSupported() throws Exception {
* not receive any early messages.
*/
assertThat(channel.receive(100)).isNull();
assertThat(channel.receive(10000)).isNotNull();
assertThat(channel.receive(20000)).isNotNull();
assertThat(idles.await(10, TimeUnit.SECONDS)).isTrue();
adapter.stop();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -28,11 +28,10 @@
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanFactory;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
Expand All @@ -57,8 +56,8 @@ public void receiveAndDelete() throws Exception {
when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER));
folderField.set(receiver, folder);

Message msg1 = spy(GreenMailUtil.newMimeMessage("test1"));
Message msg2 = spy(GreenMailUtil.newMimeMessage("test2"));
Message msg1 = GreenMailUtil.newMimeMessage("test1");
Message msg2 = GreenMailUtil.newMimeMessage("test2");
final Message[] messages = new Message[] {msg1, msg2};
doAnswer(invocation -> {
DirectFieldAccessor accessor = new DirectFieldAccessor(invocation.getMock());
Expand All @@ -74,8 +73,9 @@ public void receiveAndDelete() throws Exception {
doAnswer(invocation -> null).when(receiver).fetchMessages(messages);
receiver.afterPropertiesSet();
receiver.receive();
verify(msg1, times(1)).setFlag(Flag.DELETED, true);
verify(msg2, times(1)).setFlag(Flag.DELETED, true);

assertThat(msg1.getFlags().contains(Flag.DELETED)).isTrue();
assertThat(msg2.getFlags().contains(Flag.DELETED)).isTrue();
}

@Test
Expand All @@ -92,8 +92,8 @@ public void receiveAndDontDelete() throws Exception {
when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER));
folderField.set(receiver, folder);

Message msg1 = spy(GreenMailUtil.newMimeMessage("test1"));
Message msg2 = spy(GreenMailUtil.newMimeMessage("test2"));
Message msg1 = GreenMailUtil.newMimeMessage("test1");
Message msg2 = GreenMailUtil.newMimeMessage("test2");
final Message[] messages = new Message[] {msg1, msg2};
doAnswer(invocation -> null).when(receiver).openFolder();

Expand All @@ -102,8 +102,9 @@ public void receiveAndDontDelete() throws Exception {
doAnswer(invocation -> null).when(receiver).fetchMessages(messages);
receiver.afterPropertiesSet();
receiver.receive();
verify(msg1, times(0)).setFlag(Flag.DELETED, true);
verify(msg2, times(0)).setFlag(Flag.DELETED, true);

assertThat(msg1.getFlags().contains(Flag.DELETED)).isFalse();
assertThat(msg2.getFlags().contains(Flag.DELETED)).isFalse();
}

@Test
Expand All @@ -119,8 +120,8 @@ public void receiveAndDontSetDeleteWithUrl() throws Exception {
when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER));
folderField.set(receiver, folder);

Message msg1 = spy(GreenMailUtil.newMimeMessage("test1"));
Message msg2 = spy(GreenMailUtil.newMimeMessage("test2"));
Message msg1 = GreenMailUtil.newMimeMessage("test1");
Message msg2 = GreenMailUtil.newMimeMessage("test2");
final Message[] messages = new Message[] {msg1, msg2};
doAnswer(invocation -> null).when(receiver).openFolder();

Expand All @@ -129,8 +130,9 @@ public void receiveAndDontSetDeleteWithUrl() throws Exception {
doAnswer(invocation -> null).when(receiver).fetchMessages(messages);
receiver.afterPropertiesSet();
receiver.receive();
verify(msg1, times(0)).setFlag(Flag.DELETED, true);
verify(msg2, times(0)).setFlag(Flag.DELETED, true);

assertThat(msg1.getFlags().contains(Flag.DELETED)).isFalse();
assertThat(msg2.getFlags().contains(Flag.DELETED)).isFalse();
}

@Test
Expand All @@ -146,8 +148,8 @@ public void receiveAndDontSetDeleteWithoutUrl() throws Exception {
when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER));
folderField.set(receiver, folder);

Message msg1 = spy(GreenMailUtil.newMimeMessage("test1"));
Message msg2 = spy(GreenMailUtil.newMimeMessage("test2"));
Message msg1 = GreenMailUtil.newMimeMessage("test1");
Message msg2 = GreenMailUtil.newMimeMessage("test2");
final Message[] messages = new Message[] {msg1, msg2};
doAnswer(invocation -> null).when(receiver).openFolder();

Expand All @@ -156,8 +158,9 @@ public void receiveAndDontSetDeleteWithoutUrl() throws Exception {
doAnswer(invocation -> null).when(receiver).fetchMessages(messages);
receiver.afterPropertiesSet();
receiver.receive();
verify(msg1, times(0)).setFlag(Flag.DELETED, true);
verify(msg2, times(0)).setFlag(Flag.DELETED, true);

assertThat(msg1.getFlags().contains(Flag.DELETED)).isFalse();
assertThat(msg2.getFlags().contains(Flag.DELETED)).isFalse();
}

}

0 comments on commit 5e34809

Please sign in to comment.