Repository navigation
[Tests] Add coverage for PlayerLoadTask and PlayerUnloadTask event dispatch and onCancel paths #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,9 @@ | |
| import org.mockito.junit.jupiter.MockitoSettings; | ||
| import org.mockito.quality.Strictness; | ||
|
|
||
| import org.bukkit.plugin.PluginManager; | ||
| import org.mockito.ArgumentCaptor; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import java.time.Clock; | ||
|
|
@@ -33,6 +36,8 @@ | |
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| import com.diamonddagger590.mccore.event.player.PlayerUnloadEvent; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
|
|
@@ -42,7 +47,9 @@ | |
| import static org.mockito.ArgumentMatchers.anyLong; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.doNothing; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.lenient; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.mockStatic; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
@@ -283,4 +290,69 @@ void onIntervalComplete_runtimeException_propagates() { | |
| // runUnloadPlayerTask only catches SQLException, so RuntimeException propagates | ||
| org.junit.jupiter.api.Assertions.assertThrows(RuntimeException.class, task::onIntervalComplete); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Given successful unload, when event dispatch Runnable executes, then PlayerUnloadEvent is fired with correct player") | ||
| void onPlayerUnloadSuccessfully_eventDispatchRunnable_firesPlayerUnloadEvent() throws SQLException { | ||
| when(mockDatabase.getConnection()).thenReturn(mockConnection); | ||
| when(mockCorePlayer.useMutex()).thenReturn(false); | ||
| doNothing().when(mockScheduler).cancelTask(anyInt()); | ||
|
|
||
| PluginManager mockPluginManager = mock(PluginManager.class); | ||
| bukkitStatic.when(Bukkit::getPluginManager).thenReturn(mockPluginManager); | ||
|
|
||
| ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class); | ||
| when(mockScheduler.scheduleSyncDelayedTask(eq(mockPlugin), runnableCaptor.capture())).thenReturn(1); | ||
|
|
||
| unloadPlayerResult = true; | ||
| PlayerUnloadTask task = createTask(); | ||
| task.onIntervalComplete(); | ||
|
|
||
| assertTrue(task.getResult().isDone()); | ||
| assertTrue(task.getResult().join()); | ||
|
|
||
| runnableCaptor.getValue().run(); | ||
|
|
||
| ArgumentCaptor<PlayerUnloadEvent> eventCaptor = ArgumentCaptor.forClass(PlayerUnloadEvent.class); | ||
| verify(mockPluginManager).callEvent(eventCaptor.capture()); | ||
| assertSame(mockCorePlayer, eventCaptor.getValue().getCorePlayer()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Given task completed successfully, when run triggers onCancel, then result remains true") | ||
| void onCancel_alreadyCompleted_resultRemainsTrue() throws SQLException { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same structural issue as its Suggest verifying via a spy that |
||
| when(mockDatabase.getConnection()).thenReturn(mockConnection); | ||
| when(mockCorePlayer.useMutex()).thenReturn(false); | ||
| doNothing().when(mockScheduler).cancelTask(anyInt()); | ||
| when(mockScheduler.scheduleSyncDelayedTask(eq(mockPlugin), any(Runnable.class))).thenReturn(1); | ||
|
|
||
| unloadPlayerResult = true; | ||
| PlayerUnloadTask task = createTask(); | ||
| task.onIntervalComplete(); | ||
|
|
||
| assertTrue(task.getResult().isDone()); | ||
| assertTrue(task.getResult().join()); | ||
|
|
||
| task.run(); | ||
|
|
||
| assertTrue(task.getResult().isDone()); | ||
| assertTrue(task.getResult().join()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Given connection close throws SQLException, when onIntervalComplete, then exception is caught gracefully") | ||
| void onIntervalComplete_sqlExceptionOnClose_caughtGracefully() throws SQLException { | ||
| when(mockDatabase.getConnection()).thenReturn(mockConnection); | ||
| when(mockCorePlayer.useMutex()).thenReturn(false); | ||
| doNothing().when(mockScheduler).cancelTask(anyInt()); | ||
| when(mockScheduler.scheduleSyncDelayedTask(eq(mockPlugin), any(Runnable.class))).thenReturn(1); | ||
| doThrow(new SQLException("Connection close failed")).when(mockConnection).close(); | ||
|
|
||
| unloadPlayerResult = true; | ||
| PlayerUnloadTask task = createTask(); | ||
| task.onIntervalComplete(); | ||
|
|
||
| assertTrue(task.getResult().isDone()); | ||
| assertTrue(task.getResult().join()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test asserts
task.getResult().join()is stilltrueafterrun()triggersonCancel(), butCompletableFuture.complete()is a no-op once already completed —PlayerLoadTask.onPlayerLoadSuccessfully()already calledresult.complete(true)beforeonCancel()runs. So even if theif (!completed)guard inPlayerLoadTask.onCancel()(lines 105-109) were deleted andonPlayerLoadFail()called unconditionally,result.complete(false)would also just no-op and this assertion would still pass. The test doesn't actually exercise the guard it's meant to verify.Suggest asserting via a spy instead, e.g.: