@@ -150,7 +150,7 @@ PersistentTask<Vehicle> task1(VehicleHttpConnector vehicleHttpConnector) {
150150 .id(" my-id" ) // will overwrite existing triggers
151151 .state(new Vehicle (" funny" ))
152152 .runAfter(Duration . ofHours(2 ))
153- .build()
153+ .build();
154154
155155 triggerService. queue(trigger);
156156 }
@@ -169,102 +169,10 @@ PersistentTask<Vehicle> task1(VehicleHttpConnector vehicleHttpConnector) {
169169 }
170170```
171171
172- ### Triggers and Tasks in JUnit Tests
172+ ### JUnit Tests
173173
174- The `SchedulerService ` can be disabled for unit testing, which ensures that no trigger will be
175- executed automatically.
174+ - [Persistent Task and Testing ](https: // github.com/sterlp/spring-persistent-tasks/wiki/Triggers-and-Tasks-in-JUnit-Tests)
176175
177- ```yml
178- spring:
179- persistent- tasks:
180- scheduler- enabled: false
181- ```
182-
183- Now you can run any trigger manually using the `TriggerService `
184-
185- ```java
186- @Autowired
187- private TriggerService triggerService;
188-
189- @Test
190- void testRunTriggerDirectly() {
191- // GIVEN
192- // setup your test and create any triggers needed
193-
194- // WHEN run any pending triggers
195- triggerService. run(triggerService. queue(trigger));
196-
197- // THEN
198- // any asserts you might need
199- }
200-
201- @Test
202- void testRunUnknownTriggersCreated() {
203- // GIVEN
204- // setup your test call any method which might create triggers
205-
206- // WHEN run any pending triggers
207- triggerService. run(triggerService. lockNextTrigger(" test" ));
208-
209- // THEN
210- // any asserts you might need
211- }
212- ```
213-
214- It is also possible to define a test scheduler and use the async way to execute any triggers (without the spring scheduler which would trigger them automatically):
215-
216- ```java
217- @Configuration
218- public static class TestConfig {
219-
220- @Primary
221- @SuppressWarnings (" resource" )
222- SchedulerService schedulerService (TriggerService triggerService , EditSchedulerStatusComponent editSchedulerStatus ,
223- TransactionTemplate trx ) throws UnknownHostException {
224-
225- final var taskExecutor = new TaskExecutorComponent (triggerService, 10 );
226- taskExecutor. setMaxShutdownWaitTime(Duration . ofSeconds(0 ));
227- return new SchedulerService (" testScheduler" , triggerService, taskExecutor, editSchedulerStatus, trx);
228- }
229- }
230- ```
231-
232- Now the `PersistentTaskService ` has a method to trigger or to trigger and to wait for the result:
233-
234- ```java
235- @Autowired
236- private PersistentTaskService persistentTaskService;
237-
238- @Test
239- void testFoo() {
240- // GIVEN
241- // setup your test and create any triggers needed
242-
243- // WHEN run any pending triggers
244- persistentTaskService. executeTriggersAndWait();
245-
246- // THEN
247- // any asserts you might need
248- }
249- ```
250-
251- During the setup and cleanup it is possible to cancel any pending triggers:
252-
253- ```java
254- @BeforeEach
255- public void beforeEach() throws Exception {
256- triggerService. deleteAll();
257- historyService. deleteAll();
258- schedulerService. setMaxThreads(10 );
259- schedulerService. start();
260- }
261-
262- @AfterEach
263- public void afterEach() throws Exception {
264- // will cancel any pending tasks
265- schedulerService. shutdownNow(); // use .stop() if you want to wait
266- }
267- ```
268176
269177### Spring configuration options
270178
0 commit comments