Skip to content

Commit

Permalink
Fix disposable bean lifecycle in JobScope test utilities
Browse files Browse the repository at this point in the history
Before this commit, the destroy method of a job-scoped
bean was not called after a test method.

This commit changes the listener to respect the
DisposableBean contract for job-scoped beans (and make it
consistent with the calls to the JobSynchronizationManager
in AbstractJob, ie calling register/release).

FTR, I did not find a clean way to test this with an
assertion (which should be made after the test method),
but a log message in the destroy method shows that the
method is now called as expected.

Resolves #1288
  • Loading branch information
fmbenhassine committed Nov 21, 2023
1 parent 03d2833 commit ad50599
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void beforeTestMethod(org.springframework.test.context.TestContext testCo
@Override
public void afterTestMethod(TestContext testContext) {
if (testContext.hasAttribute(JOB_EXECUTION)) {
JobSynchronizationManager.close();
JobSynchronizationManager.release();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2010 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,7 @@
*
* @author Dave Syer
* @author Jimmy Praet
* @author Mahmoud Ben Hassine
*/
public class JobScopeTestUtils {

Expand All @@ -37,7 +38,7 @@ public static <T> T doInJobScope(JobExecution jobExecution, Callable<T> callable
return callable.call();
}
finally {
JobSynchronizationManager.close();
JobSynchronizationManager.release();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;
import org.springframework.batch.core.JobExecution;
Expand Down Expand Up @@ -56,6 +57,8 @@ JobExecution getJobExecution() {
void testJob() throws Exception {
stream.open(new ExecutionContext());
assertEquals("foo", reader.read());
assertEquals("bar", reader.read());
assertNull(reader.read());
}

}

0 comments on commit ad50599

Please sign in to comment.