Skip to content

Commit 0cd0b8c

Browse files
committed
More mocking, fixed draft-test
1 parent d9734d6 commit 0cd0b8c

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

application/src/test/java/org/togetherjava/tjbot/commands/reminder/RemindRoutineTest.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.dv8tion.jda.api.entities.MessageEmbed;
44
import net.dv8tion.jda.api.entities.TextChannel;
5+
import net.dv8tion.jda.api.entities.User;
56
import org.jetbrains.annotations.NotNull;
67
import org.junit.jupiter.api.BeforeEach;
78
import org.junit.jupiter.api.DisplayName;
@@ -12,6 +13,7 @@
1213
import org.togetherjava.tjbot.jda.JdaTester;
1314

1415
import java.time.Instant;
16+
import java.util.concurrent.TimeUnit;
1517

1618
import static org.junit.jupiter.api.Assertions.assertEquals;
1719
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -46,6 +48,8 @@ private void triggerRoutine() {
4648
void sendsPendingReminder() {
4749
// GIVEN a pending reminder
4850
Instant remindAt = Instant.now();
51+
String reminderContent = "foo";
52+
User author = jdaTester.getMemberSpy().getUser();
4953
rawReminders.insertReminder("foo", remindAt);
5054

5155
// WHEN running the routine
@@ -55,9 +59,14 @@ void sendsPendingReminder() {
5559
assertTrue(rawReminders.readReminders().isEmpty());
5660

5761
MessageEmbed lastMessage = getLastMessageFrom(jdaTester.getTextChannelSpy());
58-
assertEquals("foo", lastMessage.getDescription());
59-
assertEquals(remindAt, lastMessage.getTimestamp().toInstant());
60-
// TODO Use actual name of the test user
61-
assertEquals("name", lastMessage.getAuthor().getName());
62+
assertEquals(reminderContent, lastMessage.getDescription());
63+
assertSimilar(remindAt, lastMessage.getTimestamp().toInstant());
64+
assertEquals(author.getAsTag(), lastMessage.getAuthor().getName());
65+
}
66+
67+
private static void assertSimilar(@NotNull Instant expected, @NotNull Instant actual) {
68+
// NOTE For some reason, the instant ends up in the database slightly wrong already (about
69+
// half a second), seems to be an issue with jOOQ
70+
assertEquals(expected.toEpochMilli(), actual.toEpochMilli(), TimeUnit.SECONDS.toMillis(1));
6271
}
6372
}

application/src/test/java/org/togetherjava/tjbot/jda/JdaTester.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.concurrent.ScheduledExecutorService;
3838
import java.util.concurrent.ScheduledThreadPoolExecutor;
3939
import java.util.function.Consumer;
40+
import java.util.function.Function;
4041
import java.util.function.Supplier;
4142
import java.util.function.UnaryOperator;
4243

@@ -123,6 +124,7 @@ public JdaTester() {
123124
doReturn(APPLICATION_ID).when(selfUser).getApplicationIdLong();
124125
doReturn(selfUser).when(jda).getSelfUser();
125126
when(jda.getGuildChannelById(anyLong())).thenReturn(textChannel);
127+
when(jda.getTextChannelById(anyLong())).thenReturn(textChannel);
126128
when(jda.getPrivateChannelById(anyLong())).thenReturn(privateChannel);
127129
when(jda.getGuildById(anyLong())).thenReturn(guild);
128130
when(jda.getEntityBuilder()).thenReturn(entityBuilder);
@@ -147,13 +149,19 @@ public JdaTester() {
147149
auditableRestAction = (AuditableRestActionImpl<Void>) mock(AuditableRestActionImpl.class);
148150
doNothing().when(auditableRestAction).queue();
149151

150-
doNothing().when(messageAction).queue();
151-
152152
doReturn(everyoneRole).when(guild).getPublicRole();
153153
doReturn(selfMember).when(guild).getMember(selfUser);
154154
doReturn(member).when(guild).getMember(not(eq(selfUser)));
155155

156+
RestAction<User> userAction = createSucceededActionMock(member.getUser());
157+
when(jda.retrieveUserById(anyLong())).thenReturn(userAction);
158+
156159
doReturn(null).when(textChannel).retrieveMessageById(any());
160+
doReturn(messageAction).when(textChannel).sendMessageEmbeds(any(), any());
161+
doReturn(messageAction).when(textChannel).sendMessageEmbeds(any());
162+
163+
doNothing().when(messageAction).queue();
164+
when(messageAction.content(any())).thenReturn(messageAction);
157165
}
158166

159167
/**
@@ -319,11 +327,25 @@ public JdaTester() {
319327
successConsumer.accept(t);
320328
return null;
321329
};
330+
Answer<RestAction<?>> mapExecution = invocation -> {
331+
Function<? super T, ?> mapFunction = invocation.getArgument(0);
332+
Object result = mapFunction.apply(t);
333+
return createSucceededActionMock(result);
334+
};
335+
Answer<RestAction<?>> flatMapExecution = invocation -> {
336+
Function<? super T, RestAction<?>> flatMapFunction = invocation.getArgument(0);
337+
return flatMapFunction.apply(t);
338+
};
322339

323340
doNothing().when(action).queue();
324341

325342
doAnswer(successExecution).when(action).queue(any());
326343
doAnswer(successExecution).when(action).queue(any(), any());
344+
when(action.onErrorMap(any())).thenReturn(action);
345+
when(action.onErrorMap(any(), any())).thenReturn(action);
346+
347+
doAnswer(mapExecution).when(action).map(any());
348+
doAnswer(flatMapExecution).when(action).flatMap(any());
327349

328350
return action;
329351
}

0 commit comments

Comments
 (0)