diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/JobLauncherTestUtils.java b/spring-batch-test/src/main/java/org/springframework/batch/test/JobLauncherTestUtils.java index 5fc3dec28..05440c466 100644 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/JobLauncherTestUtils.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/JobLauncherTestUtils.java @@ -16,6 +16,7 @@ package org.springframework.batch.test; +import java.security.SecureRandom; import java.util.HashMap; import java.util.Map; @@ -160,7 +161,7 @@ public class JobLauncherTestUtils { */ public JobParameters getUniqueJobParameters() { Map parameters = new HashMap<>(); - parameters.put("random", new JobParameter((long) (Math.random() * JOB_PARAMETER_MAXIMUM))); + parameters.put("random", new JobParameter(new SecureRandom().nextLong())); return new JobParameters(parameters); } diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/JobLauncherTestUtilsTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/JobLauncherTestUtilsTests.java index 3920f1ce8..536e61a37 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/JobLauncherTestUtilsTests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/JobLauncherTestUtilsTests.java @@ -20,6 +20,7 @@ import org.junit.Test; import org.springframework.batch.core.ExitStatus; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.Step; import org.springframework.batch.core.StepContribution; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; @@ -35,7 +36,11 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.lang.Nullable; +import java.util.HashSet; +import java.util.Set; + import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; /** * @author mminella @@ -53,6 +58,18 @@ public class JobLauncherTestUtilsTests { assertEquals(ExitStatus.COMPLETED, execution.getExitStatus()); } + @Test + public void getUniqueJobParameters_doesNotRepeatJobParameters() { + ApplicationContext context = new AnnotationConfigApplicationContext(TestJobConfiguration.class); + JobLauncherTestUtils testUtils = context.getBean(JobLauncherTestUtils.class); + Set jobParametersSeen = new HashSet<>(); + for (int i = 0; i < 10_000; i++) { + JobParameters jobParameters = testUtils.getUniqueJobParameters(); + assertFalse(jobParametersSeen.contains(jobParameters)); + jobParametersSeen.add(jobParameters); + } + } + @Configuration @EnableBatchProcessing public static class TestJobConfiguration {