Skip to content

Commit 0c99bcd

Browse files
committed
Revising TaskHandlerRegistry unit tests and remaining handler etc tests
1 parent 768aa27 commit 0c99bcd

5 files changed

Lines changed: 62 additions & 46 deletions

File tree

springqpro-backend/src/test/java/com/springqprobackend/springqpro/handlers/DefaultHandlerTests.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,46 @@
1414
import org.mockito.Mock;
1515
import org.mockito.junit.jupiter.MockitoExtension;
1616

17+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1718
import static org.junit.jupiter.api.Assertions.assertEquals;
18-
import static org.mockito.Mockito.mock;
19-
import static org.mockito.Mockito.when;
19+
import static org.mockito.Mockito.*;
2020

2121
/* NOTE: I basically only have two types of Handlers defined as of this moment.
2222
The variation you see in DefaultHandler mimicked across other Handlers w/ different sleep times.
2323
-- Picking DefaultHandler specifically since it's the *default* Handler I'll always have regardless of refactoring, etc.
2424
*/
2525
@ExtendWith(MockitoExtension.class)
26-
public class DefaultHandlerTests {
27-
private Sleeper fastSleeper;
28-
private DefaultHandler handler;
29-
private Task t;
26+
class DefaultHandlerTests {
27+
28+
@Mock
29+
private Sleeper sleeper;
3030

3131
@Mock
3232
private TaskHandlerProperties props;
3333

34+
private DefaultHandler handler;
35+
36+
private Task task;
37+
3438
@BeforeEach
3539
void setUp() {
36-
/* NOTE: Very important is the line of code beneath this comment!
37-
We're basically overriding the "RealSleeper" implementation of the Sleeper interface here
38-
by using fastSleeper instead. Below is a lambda expression that implements Sleeper (functional interface),
39-
which says that this Sleeper implementation takes some "long" var and just does nothing (doesn't actually sleep).
40-
So we essentially skip the .sleep part of the Handler execution and skip straight to the followup logic. */
41-
fastSleeper = millis -> {};
4240
when(props.getDefaultSleepTime()).thenReturn(2000L);
43-
handler = new DefaultHandler(fastSleeper, props);
44-
t = new Task();
45-
t.setId("Task-ArbitraryTaskId");
46-
t.setType(TaskType.valueOf("TEST"));
41+
handler = new DefaultHandler(sleeper, props);
42+
43+
task = new Task();
44+
task.setId("Task-ArbitraryTaskId");
45+
task.setType(TaskType.EMAIL);
46+
}
47+
48+
@Test
49+
void handle_shouldInvokeSleeperWithConfiguredDelay() throws InterruptedException {
50+
handler.handle(task);
51+
52+
verify(sleeper).sleep(2000L);
4753
}
4854

49-
/* 2025-11-20-EDIT: This file and DefaultHandlerTests.java is outdated, relying on the old project architecture
50-
before any of the refactoring related to ProcessingService.java */
51-
@Disabled
5255
@Test
53-
void handle_shouldSet_TaskCompleted() throws InterruptedException {
54-
handler.handle(t);
55-
assertEquals(TaskStatus.COMPLETED, t.getStatus());
56+
void handle_shouldNotThrow() {
57+
assertDoesNotThrow(() -> handler.handle(task));
5658
}
5759
}

springqpro-backend/src/test/java/com/springqprobackend/springqpro/handlers/FailHandlerTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.springqprobackend.springqpro.handlers;
22

3+
// 2026-01-16-NOTE: Reinforcing the comment below; handlers just simulate worktime nothing else (deprecating this file).
4+
35
// 2025-11-30-NOTE: Basically shouldn't be testing Handlers anymore. This is kept for my documentation stuff.
46

57
/* NOTE: Testing my FailHandler implementation of the TaskHandler class is a little bit tricky.
@@ -30,6 +32,7 @@ Now, instead of declaring a new Random() variable in function, it's a class fiel
3032
import static org.junit.jupiter.api.Assertions.assertEquals;
3133
import static org.mockito.Mockito.*;
3234

35+
@Deprecated
3336
@ExtendWith(MockitoExtension.class)
3437
public class FailHandlerTests {
3538
@Mock

springqpro-backend/src/test/java/com/springqprobackend/springqpro/models/TaskHandlerRegistryTests.java

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ implement the interface are injected. That means that no mocks are needed here (
77
import com.springqprobackend.springqpro.handlers.TaskHandler;
88
import org.junit.jupiter.api.BeforeEach;
99
import org.junit.jupiter.api.Test;
10+
import org.mockito.Mock;
11+
import org.mockito.MockitoAnnotations;
1012

1113
import java.util.HashMap;
1214
import java.util.Map;
1315

16+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
1417
import static org.junit.jupiter.api.Assertions.assertEquals;
1518
import static org.mockito.Mockito.mock;
1619

@@ -19,44 +22,48 @@ implement the interface are injected. That means that no mocks are needed here (
1922
- Returns DefaultHandler if type is missing.
2023
- getAllHandlers() exposes all of the handlers.
2124
*/
22-
public class TaskHandlerRegistryTests {
25+
class TaskHandlerRegistryTests {
26+
27+
@Mock
2328
private TaskHandler defaultHandler;
24-
private Map<String, TaskHandler> map;
2529

2630
@BeforeEach
2731
void setUp() {
28-
defaultHandler = mock(TaskHandler.class);
29-
//map = Map.of("DEFAULT", defaultHandler);
30-
map = new HashMap<>();
31-
map.put("DEFAULT", defaultHandler);
32+
MockitoAnnotations.openMocks(this);
3233
}
3334

3435
@Test
35-
void getHandler_returns_correctHandler() {
36+
void getHandler_returnsCorrectHandler_whenHandlerExists() {
3637
TaskHandler emailHandler = mock(TaskHandler.class);
37-
/*TaskHandler defaultHandler = mock(TaskHandler.class);
38-
Map<String, TaskHandler> map = Map.of(
39-
"EMAIL", emailHandler,
40-
"DEFAULT", defaultHandler
41-
);*/
42-
map.put("EMAIL", emailHandler);
43-
TaskHandlerRegistry registry = new TaskHandlerRegistry(map);
44-
assertEquals(emailHandler, registry.getHandler("EMAIL"));
38+
39+
Map<String, TaskHandler> handlers = new HashMap<>();
40+
handlers.put("DEFAULT", defaultHandler);
41+
handlers.put("EMAIL", emailHandler);
42+
43+
TaskHandlerRegistry registry = new TaskHandlerRegistry(handlers);
44+
45+
assertThat(registry.getHandler("EMAIL")).isEqualTo(emailHandler);
4546
}
4647

4748
@Test
48-
void unknownHandler_defaultsTo_defaultHandler() {
49-
/*TaskHandler defaultHandler = mock(TaskHandler.class);
50-
Map<String, TaskHandler> map = Map.of(
49+
void getHandler_fallsBackToDefault_whenHandlerIsUnknown() {
50+
Map<String, TaskHandler> handlers = Map.of(
5151
"DEFAULT", defaultHandler
52-
);*/
53-
TaskHandlerRegistry registry = new TaskHandlerRegistry(map);
54-
assertEquals(defaultHandler, registry.getHandler("IDONTKNOW"));
52+
);
53+
54+
TaskHandlerRegistry registry = new TaskHandlerRegistry(handlers);
55+
56+
assertThat(registry.getHandler("IDONTKNOW")).isEqualTo(defaultHandler);
5557
}
5658

5759
@Test
58-
void getAllHandlers_returns_injectedMap() {
59-
TaskHandlerRegistry registry = new TaskHandlerRegistry(map);
60-
assertEquals(map, registry.getAllHandlers());
60+
void getAllHandlers_returnsInjectedHandlerMap() {
61+
Map<String, TaskHandler> handlers = Map.of(
62+
"DEFAULT", defaultHandler
63+
);
64+
65+
TaskHandlerRegistry registry = new TaskHandlerRegistry(handlers);
66+
67+
assertThat(registry.getAllHandlers()).isEqualTo(handlers);
6168
}
6269
}

springqpro-backend/src/test/java/com/springqprobackend/springqpro/runtime/WorkerTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
import static org.junit.jupiter.api.Assertions.*;
1515
import static org.mockito.Mockito.*;
1616

17+
// 2026-01-16-NOTE: Completely outdated.
18+
1719
/* NOTE: This function got much simpler after adding the TaskHandlerRegistry and global exception
1820
handler. Writing tests should be relatively simple.
1921
Again:
2022
- More on different assert methods: https://www.baeldung.com/junit-assertions
2123
*/
2224
// commit test
2325

26+
@Deprecated
2427
@ExtendWith(MockitoExtension.class)
2528
public class WorkerTests {
2629
@Mock

springqpro-backend/src/test/java/com/springqprobackend/springqpro/service/QueueServiceTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- HandlerRegistry fallback — test that when no handler exists, the default handler runs.
3636
*/
3737

38+
@Deprecated
3839
@ExtendWith(MockitoExtension.class)
3940
public class QueueServiceTests {
4041

0 commit comments

Comments
 (0)