Skip to content

Commit 6117c04

Browse files
hpoettkerfmbenhassine
authored andcommitted
Migrate Spring Batch Test to JUnit Jupiter
1 parent f14d57f commit 6117c04

19 files changed

+241
-320
lines changed

spring-batch-test/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
</dependency>
6464

6565
<!-- test dependencies -->
66+
<dependency>
67+
<groupId>org.junit.vintage</groupId>
68+
<artifactId>junit-vintage-engine</artifactId>
69+
<version>${junit-vintage-engine.version}</version>
70+
<scope>test</scope>
71+
</dependency>
6672
<dependency>
6773
<groupId>org.mockito</groupId>
6874
<artifactId>mockito-core</artifactId>

spring-batch-test/src/test/java/org/springframework/batch/test/AbstractSampleJobTests.java

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2021 the original author or authors.
2+
* Copyright 2009-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,20 +15,21 @@
1515
*/
1616
package org.springframework.batch.test;
1717

18-
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertTrue;
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
2021

21-
import org.junit.After;
22-
import org.junit.Before;
23-
import org.junit.Test;
22+
import org.junit.jupiter.api.AfterEach;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.RepeatedTest;
25+
import org.junit.jupiter.api.Test;
2426
import org.springframework.batch.core.BatchStatus;
2527
import org.springframework.batch.item.ExecutionContext;
2628
import org.springframework.batch.test.sample.SampleTasklet;
2729
import org.springframework.beans.factory.annotation.Autowired;
2830
import org.springframework.beans.factory.annotation.Qualifier;
2931
import org.springframework.jdbc.core.JdbcTemplate;
30-
import org.springframework.test.annotation.Repeat;
31-
import org.springframework.test.context.ContextConfiguration;
32+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3233
import org.springframework.test.jdbc.JdbcTestUtils;
3334

3435
/**
@@ -37,9 +38,10 @@
3738
* @author Dan Garrette
3839
* @since 2.0
3940
*/
40-
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/job-runner-context.xml" })
41-
public abstract class AbstractSampleJobTests {
41+
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/job-runner-context.xml" })
42+
abstract class AbstractSampleJobTests {
4243

44+
@Autowired
4345
private JdbcTemplate jdbcTemplate;
4446

4547
@Autowired
@@ -49,56 +51,50 @@ public abstract class AbstractSampleJobTests {
4951
@Qualifier("tasklet2")
5052
private SampleTasklet tasklet2;
5153

52-
@Autowired
53-
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
54-
this.jdbcTemplate = jdbcTemplate;
55-
}
56-
57-
@Before
58-
public void setUp() {
54+
@BeforeEach
55+
void setUp() {
5956
this.jdbcTemplate.update("create table TESTS (ID integer, NAME varchar(40))");
6057
tasklet2.jobContextEntryFound = false;
6158
}
6259

63-
@After
64-
public void tearDown() {
60+
@AfterEach
61+
void tearDown() {
6562
JdbcTestUtils.dropTables(this.jdbcTemplate, "TESTS");
6663
}
6764

6865
@Test
69-
public void testJob() throws Exception {
66+
void testJob() throws Exception {
7067
assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.launchJob().getStatus());
7168
this.verifyTasklet(1);
7269
this.verifyTasklet(2);
7370
}
7471

75-
@Test(expected = IllegalStateException.class)
76-
public void testNonExistentStep() {
77-
jobLauncherTestUtils.launchStep("nonExistent");
72+
@Test
73+
void testNonExistentStep() {
74+
assertThrows(IllegalStateException.class, () -> jobLauncherTestUtils.launchStep("nonExistent"));
7875
}
7976

8077
@Test
81-
public void testStep1Execution() {
78+
void testStep1Execution() {
8279
assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.launchStep("step1").getStatus());
8380
this.verifyTasklet(1);
8481
}
8582

8683
@Test
87-
public void testStep2Execution() {
84+
void testStep2Execution() {
8885
assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.launchStep("step2").getStatus());
8986
this.verifyTasklet(2);
9087
}
9188

92-
@Test
93-
@Repeat(10)
94-
public void testStep3Execution() throws Exception {
89+
@RepeatedTest(10)
90+
void testStep3Execution() {
9591
// logging only, may complete in < 1ms (repeat so that it's likely to for at least
9692
// one of those times)
9793
assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.launchStep("step3").getStatus());
9894
}
9995

10096
@Test
101-
public void testStepLaunchJobContextEntry() {
97+
void testStepLaunchJobContextEntry() {
10298
ExecutionContext jobContext = new ExecutionContext();
10399
jobContext.put("key1", "value1");
104100
assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.launchStep("step2", jobContext).getStatus());
Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2009 the original author or authors.
2+
* Copyright 2008-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,11 +15,12 @@
1515
*/
1616
package org.springframework.batch.test;
1717

18-
import static org.junit.Assert.assertTrue;
19-
import static org.junit.Assert.fail;
18+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
2021

2122
import org.junit.ComparisonFailure;
22-
import org.junit.Test;
23+
import org.junit.jupiter.api.Test;
2324
import org.springframework.core.io.FileSystemResource;
2425

2526
/**
@@ -28,73 +29,48 @@
2829
* @author Dan Garrette
2930
* @since 2.0
3031
*/
31-
public class AssertFileTests {
32+
class AssertFileTests {
3233

3334
private static final String DIRECTORY = "src/test/resources/data/input/";
3435

3536
@Test
36-
public void testAssertEquals_equal() throws Exception {
37-
executeAssertEquals("input1.txt", "input1.txt");
37+
void testAssertEquals_equal() {
38+
assertDoesNotThrow(() -> executeAssertEquals("input1.txt", "input1.txt"));
3839
}
3940

4041
@Test
41-
public void testAssertEquals_notEqual() throws Exception {
42-
try {
43-
executeAssertEquals("input1.txt", "input2.txt");
44-
fail();
45-
}
46-
catch (ComparisonFailure e) {
47-
assertTrue(e.getMessage().startsWith("Line number 3 does not match."));
48-
}
42+
void testAssertEquals_notEqual() {
43+
Error error = assertThrows(ComparisonFailure.class, () -> executeAssertEquals("input1.txt", "input2.txt"));
44+
assertTrue(error.getMessage().startsWith("Line number 3 does not match."));
4945
}
5046

5147
@Test
52-
public void testAssertEquals_tooLong() throws Exception {
53-
try {
54-
executeAssertEquals("input3.txt", "input1.txt");
55-
fail();
56-
}
57-
catch (AssertionError e) {
58-
assertTrue(e.getMessage().startsWith("More lines than expected. There should not be a line number 4."));
59-
}
48+
void testAssertEquals_tooLong() {
49+
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("input3.txt", "input1.txt"));
50+
assertTrue(error.getMessage().startsWith("More lines than expected. There should not be a line number 4."));
6051
}
6152

6253
@Test
63-
public void testAssertEquals_tooShort() throws Exception {
64-
try {
65-
executeAssertEquals("input1.txt", "input3.txt");
66-
fail();
67-
}
68-
catch (AssertionError e) {
69-
assertTrue(e.getMessage().startsWith("Line number 4 does not match."));
70-
}
54+
void testAssertEquals_tooShort() {
55+
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("input1.txt", "input3.txt"));
56+
assertTrue(error.getMessage().startsWith("Line number 4 does not match."));
7157
}
7258

7359
@Test
74-
public void testAssertEquals_blank_equal() throws Exception {
75-
executeAssertEquals("blank.txt", "blank.txt");
60+
void testAssertEquals_blank_equal() {
61+
assertDoesNotThrow(() -> executeAssertEquals("blank.txt", "blank.txt"));
7662
}
7763

7864
@Test
79-
public void testAssertEquals_blank_tooLong() throws Exception {
80-
try {
81-
executeAssertEquals("blank.txt", "input1.txt");
82-
fail();
83-
}
84-
catch (AssertionError e) {
85-
assertTrue(e.getMessage().startsWith("More lines than expected. There should not be a line number 1."));
86-
}
65+
void testAssertEquals_blank_tooLong() {
66+
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("blank.txt", "input1.txt"));
67+
assertTrue(error.getMessage().startsWith("More lines than expected. There should not be a line number 1."));
8768
}
8869

8970
@Test
90-
public void testAssertEquals_blank_tooShort() throws Exception {
91-
try {
92-
executeAssertEquals("input1.txt", "blank.txt");
93-
fail();
94-
}
95-
catch (AssertionError e) {
96-
assertTrue(e.getMessage().startsWith("Line number 1 does not match."));
97-
}
71+
void testAssertEquals_blank_tooShort() {
72+
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("input1.txt", "blank.txt"));
73+
assertTrue(error.getMessage().startsWith("Line number 1 does not match."));
9874
}
9975

10076
private void executeAssertEquals(String expected, String actual) throws Exception {
@@ -103,8 +79,8 @@ private void executeAssertEquals(String expected, String actual) throws Exceptio
10379
}
10480

10581
@Test
106-
public void testAssertLineCount() throws Exception {
107-
AssertFile.assertLineCount(5, new FileSystemResource(DIRECTORY + "input1.txt"));
82+
void testAssertLineCount() {
83+
assertDoesNotThrow(() -> AssertFile.assertLineCount(5, new FileSystemResource(DIRECTORY + "input1.txt")));
10884
}
10985

11086
}

spring-batch-test/src/test/java/org/springframework/batch/test/ExecutionContextTestUtilsTests.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2009 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,19 +16,20 @@
1616

1717
package org.springframework.batch.test;
1818

19-
import static org.junit.Assert.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
2021

2122
import java.util.Arrays;
2223
import java.util.Date;
2324

24-
import org.junit.Test;
25+
import org.junit.jupiter.api.Test;
2526
import org.springframework.batch.core.JobExecution;
2627
import org.springframework.batch.core.StepExecution;
2728

28-
public class ExecutionContextTestUtilsTests {
29+
class ExecutionContextTestUtilsTests {
2930

3031
@Test
31-
public void testFromJob() throws Exception {
32+
void testFromJob() {
3233
Date date = new Date();
3334
JobExecution jobExecution = MetaDataInstanceFactory.createJobExecution();
3435
jobExecution.getExecutionContext().put("foo", date);
@@ -37,7 +38,7 @@ public void testFromJob() throws Exception {
3738
}
3839

3940
@Test
40-
public void testFromStepInJob() throws Exception {
41+
void testFromStepInJob() {
4142
Date date = new Date();
4243
JobExecution jobExecution = MetaDataInstanceFactory.createJobExecutionWithStepExecutions(123L,
4344
Arrays.asList("foo", "bar"));
@@ -47,17 +48,16 @@ public void testFromStepInJob() throws Exception {
4748
assertEquals(date, result);
4849
}
4950

50-
@Test(expected = IllegalArgumentException.class)
51-
public void testFromStepInJobNoSuchStep() throws Exception {
52-
Date date = new Date();
51+
@Test
52+
void testFromStepInJobNoSuchStep() {
5353
JobExecution jobExecution = MetaDataInstanceFactory.createJobExecutionWithStepExecutions(123L,
5454
Arrays.asList("foo", "bar"));
55-
Date result = ExecutionContextTestUtils.getValueFromStepInJob(jobExecution, "spam", "foo");
56-
assertEquals(date, result);
55+
assertThrows(IllegalArgumentException.class,
56+
() -> ExecutionContextTestUtils.getValueFromStepInJob(jobExecution, "spam", "foo"));
5757
}
5858

5959
@Test
60-
public void testFromStep() throws Exception {
60+
void testFromStep() {
6161
Date date = new Date();
6262
StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution();
6363
stepExecution.getExecutionContext().put("foo", date);

spring-batch-test/src/test/java/org/springframework/batch/test/JobLauncherTestUtilsTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2021 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.batch.test;
1717

18-
import org.junit.Test;
18+
import org.junit.jupiter.api.Test;
1919

2020
import org.springframework.batch.core.ExitStatus;
2121
import org.springframework.batch.core.Job;
@@ -42,17 +42,17 @@
4242

4343
import javax.sql.DataSource;
4444

45-
import static org.junit.Assert.assertEquals;
46-
import static org.junit.Assert.assertFalse;
45+
import static org.junit.jupiter.api.Assertions.assertEquals;
46+
import static org.junit.jupiter.api.Assertions.assertFalse;
4747

4848
/**
4949
* @author mminella
5050
* @author Mahmoud Ben Hassine
5151
*/
52-
public class JobLauncherTestUtilsTests {
52+
class JobLauncherTestUtilsTests {
5353

5454
@Test
55-
public void testStepExecutionWithJavaConfig() {
55+
void testStepExecutionWithJavaConfig() {
5656
ApplicationContext context = new AnnotationConfigApplicationContext(TestJobConfiguration.class);
5757

5858
JobLauncherTestUtils testUtils = context.getBean(JobLauncherTestUtils.class);
@@ -63,7 +63,7 @@ public void testStepExecutionWithJavaConfig() {
6363
}
6464

6565
@Test
66-
public void getUniqueJobParameters_doesNotRepeatJobParameters() {
66+
void getUniqueJobParameters_doesNotRepeatJobParameters() {
6767
ApplicationContext context = new AnnotationConfigApplicationContext(TestJobConfiguration.class);
6868
JobLauncherTestUtils testUtils = context.getBean(JobLauncherTestUtils.class);
6969
Set<JobParameters> jobParametersSeen = new HashSet<>();
@@ -76,7 +76,7 @@ public void getUniqueJobParameters_doesNotRepeatJobParameters() {
7676

7777
@Configuration
7878
@EnableBatchProcessing
79-
public static class TestJobConfiguration {
79+
static class TestJobConfiguration {
8080

8181
@Autowired
8282
public JobBuilderFactory jobBuilderFactory;

0 commit comments

Comments
 (0)