|
1 | 1 | package world.bentobox.challenges.tasks;
|
2 | 2 |
|
| 3 | +import static org.junit.Assert.assertEquals; |
3 | 4 | import static org.junit.Assert.assertFalse;
|
4 | 5 | import static org.junit.Assert.assertTrue;
|
5 | 6 | import static org.mockito.ArgumentMatchers.any;
|
6 | 7 | import static org.mockito.ArgumentMatchers.anyString;
|
7 | 8 | import static org.mockito.ArgumentMatchers.eq;
|
| 9 | +import static org.mockito.Mockito.atLeast; |
8 | 10 | import static org.mockito.Mockito.mock;
|
9 | 11 | import static org.mockito.Mockito.never;
|
10 | 12 | import static org.mockito.Mockito.verify;
|
|
30 | 32 | import org.bukkit.entity.Entity;
|
31 | 33 | import org.bukkit.entity.EntityType;
|
32 | 34 | import org.bukkit.entity.Player;
|
| 35 | +import org.bukkit.entity.Player.Spigot; |
33 | 36 | import org.bukkit.inventory.ItemFactory;
|
34 | 37 | import org.bukkit.inventory.ItemStack;
|
35 | 38 | import org.bukkit.inventory.PlayerInventory;
|
|
41 | 44 | import org.junit.Ignore;
|
42 | 45 | import org.junit.Test;
|
43 | 46 | import org.junit.runner.RunWith;
|
| 47 | +import org.mockito.ArgumentCaptor; |
44 | 48 | import org.mockito.Mock;
|
45 | 49 | import org.mockito.Mockito;
|
46 | 50 | import org.mockito.stubbing.Answer;
|
|
49 | 53 | import org.powermock.modules.junit4.PowerMockRunner;
|
50 | 54 | import org.powermock.reflect.Whitebox;
|
51 | 55 |
|
| 56 | +import net.md_5.bungee.api.chat.TextComponent; |
52 | 57 | import world.bentobox.bentobox.BentoBox;
|
53 | 58 | import world.bentobox.bentobox.api.addons.AddonDescription;
|
54 | 59 | import world.bentobox.bentobox.api.addons.GameModeAddon;
|
@@ -119,6 +124,8 @@ public class TryToCompleteTest {
|
119 | 124 | @Mock
|
120 | 125 | private BoundingBox bb;
|
121 | 126 | private Set<Player> onlinePlayers;
|
| 127 | + @Mock |
| 128 | + private Spigot spigot; |
122 | 129 |
|
123 | 130 | /**
|
124 | 131 | */
|
@@ -207,6 +214,7 @@ public void setUp() {
|
207 | 214 | when(user.getPlayer()).thenReturn(player);
|
208 | 215 | UUID uniqueId = UUID.randomUUID();
|
209 | 216 | when(player.getUniqueId()).thenReturn(uniqueId);
|
| 217 | + when(player.spigot()).thenReturn(spigot); |
210 | 218 | when(user.getUniqueId()).thenReturn(uniqueId);
|
211 | 219 | when(user.getTranslation(anyString()))
|
212 | 220 | .thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
|
@@ -702,4 +710,32 @@ public void testRemoveItemsNothing() {
|
702 | 710 |
|
703 | 711 | }
|
704 | 712 |
|
| 713 | + /** |
| 714 | + * Check that spigot sent the message |
| 715 | + * @param message - message to check |
| 716 | + */ |
| 717 | + public void checkSpigotMessage(String expectedMessage) { |
| 718 | + checkSpigotMessage(expectedMessage, 1); |
| 719 | + } |
| 720 | + |
| 721 | + public void checkSpigotMessage(String expectedMessage, int expectedOccurrences) { |
| 722 | + // Capture the argument passed to spigot().sendMessage(...) if messages are sent |
| 723 | + ArgumentCaptor<TextComponent> captor = ArgumentCaptor.forClass(TextComponent.class); |
| 724 | + |
| 725 | + // Verify that sendMessage() was called at least 0 times (capture any sent messages) |
| 726 | + verify(spigot, atLeast(0)).sendMessage(captor.capture()); |
| 727 | + |
| 728 | + // Get all captured TextComponents |
| 729 | + List<TextComponent> capturedMessages = captor.getAllValues(); |
| 730 | + |
| 731 | + // Count the number of occurrences of the expectedMessage in the captured messages |
| 732 | + long actualOccurrences = capturedMessages.stream().map(component -> component.toLegacyText()) // Convert each TextComponent to plain text |
| 733 | + .filter(messageText -> messageText.contains(expectedMessage)) // Check if the message contains the expected text |
| 734 | + .count(); // Count how many times the expected message appears |
| 735 | + |
| 736 | + // Assert that the number of occurrences matches the expectedOccurrences |
| 737 | + assertEquals("Expected message occurrence mismatch: " + expectedMessage, expectedOccurrences, |
| 738 | + actualOccurrences); |
| 739 | + } |
| 740 | + |
705 | 741 | }
|
0 commit comments