Skip to content

Commit

Permalink
test: fix broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pupskuchen committed Apr 5, 2023
1 parent f57b64f commit 552d76c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void createSkipperAndStartGuard() {
NightSkipper skipper = mock.constructed().get(0);

verify(logger, times(1)).debug("%s (@ %s) entered a bed at %d", "somePlayerName", "someWorld", 15000L);
verifyNoInteractions(skipper);
verify(skipper).scheduleSkip();
}
}

Expand All @@ -119,7 +119,7 @@ public void restartExistingGuard() {
NightSkipper skipper = mock.constructed().get(0);

verify(logger, times(2)).debug("%s (@ %s) entered a bed at %d", "somePlayerName", "someWorld", 15000L);
verifyNoInteractions(skipper);
verify(skipper, times(2)).scheduleSkip();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.pupskuchen.timecontrol.nightskipping;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
Expand Down Expand Up @@ -35,16 +36,13 @@ public class NightSkipperTest {
@Mock
final World world = mock(World.class);

private NightSkipper skipper;

List<List<Player>> players = List.of(this.getPlayers(0, 50, 0), this.getPlayers(100, 50, 0),
this.getPlayers(100, 100, 50), this.getPlayers(100, 100, 100));

@BeforeEach
public void setup() {
when(plugin.getTCLogger()).thenReturn(logger);
when(plugin.getConfigHandler()).thenReturn(configManager);
skipper = new NightSkipper(plugin, world);
}

private Player getPlayer(int sleepTicks) {
Expand All @@ -58,10 +56,14 @@ private List<Player> getPlayers(int... sleepTicks) {

@Test
public void skipNightNoSleepAllowed() {
when(world.getGameRuleValue(GameRule.PLAYERS_SLEEPING_PERCENTAGE)).thenReturn( 50);
final NightSkipper skipper = new NightSkipper(plugin, world);

try (MockedStatic<TimeUtil> mock = mockStatic(TimeUtil.class)) {
mock.when(() -> TimeUtil.sleepAllowed(world)).thenReturn(false);
skipper.skipNight();
verifyNoInteractions(world);
verify(world, times(0)).setTime(anyLong());
verify(world, times(0)).setTime(anyInt());
verifyNoInteractions(logger);
}
}
Expand All @@ -72,6 +74,8 @@ public void skipNightThresholdNotMet() {
when(world.getGameRuleValue(GameRule.PLAYERS_SLEEPING_PERCENTAGE)).thenReturn( 50);
when(world.getPlayers()).thenReturn(players.get(0));

final NightSkipper skipper = new NightSkipper(plugin, world);

try(MockedStatic<TimeUtil> mock = mockStatic(TimeUtil.class)) {
mock.when(() -> TimeUtil.sleepAllowed(world)).thenReturn(true);
skipper.skipNight();
Expand All @@ -86,6 +90,8 @@ public void skipNightByGameRule() {
when(world.getPlayers()).thenReturn(players.get(2));
when(world.getName()).thenReturn("fancy-world");

final NightSkipper skipper = new NightSkipper(plugin, world);

try(MockedStatic<TimeUtil> mock = mockStatic(TimeUtil.class)) {
mock.when(() -> TimeUtil.sleepAllowed(world)).thenReturn(true);
mock.when(() -> TimeUtil.getWakeTime(world)).thenReturn(123);
Expand All @@ -103,6 +109,7 @@ public void skipNightByByFallback() {
when(world.getPlayers()).thenReturn(players.get(2), players.get(3));
when(world.getName()).thenReturn("fancy-world");

final NightSkipper skipper = new NightSkipper(plugin, world);

try(MockedStatic<TimeUtil> mock = mockStatic(TimeUtil.class)) {
mock.when(() -> TimeUtil.sleepAllowed(world)).thenReturn(true);
Expand All @@ -111,9 +118,9 @@ public void skipNightByByFallback() {
verify(world, times(0)).setTime(anyLong());
skipper.skipNight();
verify(world, times(1)).setTime(123);
verify(logger, times(2)).warn("Could not fetch game-rule value 'playersSleepingPercentage!" +
" Please enable players-sleeping-percentage in the plugin configuration.");
verify(logger, times(2)).warn("Using fallback percentage of %d %%.", 100);
verify(logger, times(1)).warn("Failed to read game rule 'playersSleepingPercentage!"
+ " Please enable players-sleeping-percentage in the plugin configuration.");
verify(logger, times(1)).warn("Using fallback percentage of %d %%.", 100);

verify(logger, times(1)).info("Skipped the night on world \"%s\".", "fancy-world"); }

Expand All @@ -126,6 +133,8 @@ public void skipNightByByConfiguration() {
when(world.getPlayers()).thenReturn(players.get(2));
when(world.getName()).thenReturn("fancy-world");

final NightSkipper skipper = new NightSkipper(plugin, world);

try(MockedStatic<TimeUtil> mock = mockStatic(TimeUtil.class)) {
mock.when(() -> TimeUtil.sleepAllowed(world)).thenReturn(true);
mock.when(() -> TimeUtil.getWakeTime(world)).thenReturn(123);
Expand Down

0 comments on commit 552d76c

Please sign in to comment.