Skip to content

Migrate Spring Batch Test to JUnit Jupiter #4174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions spring-batch-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-vintage-engine.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2021 the original author or authors.
* Copyright 2009-2022 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 @@ -15,20 +15,21 @@
*/
package org.springframework.batch.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.test.sample.SampleTasklet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.annotation.Repeat;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.jdbc.JdbcTestUtils;

/**
Expand All @@ -37,9 +38,10 @@
* @author Dan Garrette
* @since 2.0
*/
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/job-runner-context.xml" })
public abstract class AbstractSampleJobTests {
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/job-runner-context.xml" })
abstract class AbstractSampleJobTests {

@Autowired
private JdbcTemplate jdbcTemplate;

@Autowired
Expand All @@ -49,56 +51,50 @@ public abstract class AbstractSampleJobTests {
@Qualifier("tasklet2")
private SampleTasklet tasklet2;

@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

@Before
public void setUp() {
@BeforeEach
void setUp() {
this.jdbcTemplate.update("create table TESTS (ID integer, NAME varchar(40))");
tasklet2.jobContextEntryFound = false;
}

@After
public void tearDown() {
@AfterEach
void tearDown() {
JdbcTestUtils.dropTables(this.jdbcTemplate, "TESTS");
}

@Test
public void testJob() throws Exception {
void testJob() throws Exception {
assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.launchJob().getStatus());
this.verifyTasklet(1);
this.verifyTasklet(2);
}

@Test(expected = IllegalStateException.class)
public void testNonExistentStep() {
jobLauncherTestUtils.launchStep("nonExistent");
@Test
void testNonExistentStep() {
assertThrows(IllegalStateException.class, () -> jobLauncherTestUtils.launchStep("nonExistent"));
}

@Test
public void testStep1Execution() {
void testStep1Execution() {
assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.launchStep("step1").getStatus());
this.verifyTasklet(1);
}

@Test
public void testStep2Execution() {
void testStep2Execution() {
assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.launchStep("step2").getStatus());
this.verifyTasklet(2);
}

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

@Test
public void testStepLaunchJobContextEntry() {
void testStepLaunchJobContextEntry() {
ExecutionContext jobContext = new ExecutionContext();
jobContext.put("key1", "value1");
assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.launchStep("step2", jobContext).getStatus());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2009 the original author or authors.
* Copyright 2008-2022 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 @@ -15,11 +15,12 @@
*/
package org.springframework.batch.test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.ComparisonFailure;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.FileSystemResource;

/**
Expand All @@ -28,73 +29,48 @@
* @author Dan Garrette
* @since 2.0
*/
public class AssertFileTests {
class AssertFileTests {

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

@Test
public void testAssertEquals_equal() throws Exception {
executeAssertEquals("input1.txt", "input1.txt");
void testAssertEquals_equal() {
assertDoesNotThrow(() -> executeAssertEquals("input1.txt", "input1.txt"));
}

@Test
public void testAssertEquals_notEqual() throws Exception {
try {
executeAssertEquals("input1.txt", "input2.txt");
fail();
}
catch (ComparisonFailure e) {
assertTrue(e.getMessage().startsWith("Line number 3 does not match."));
}
void testAssertEquals_notEqual() {
Error error = assertThrows(ComparisonFailure.class, () -> executeAssertEquals("input1.txt", "input2.txt"));
assertTrue(error.getMessage().startsWith("Line number 3 does not match."));
}

@Test
public void testAssertEquals_tooLong() throws Exception {
try {
executeAssertEquals("input3.txt", "input1.txt");
fail();
}
catch (AssertionError e) {
assertTrue(e.getMessage().startsWith("More lines than expected. There should not be a line number 4."));
}
void testAssertEquals_tooLong() {
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("input3.txt", "input1.txt"));
assertTrue(error.getMessage().startsWith("More lines than expected. There should not be a line number 4."));
}

@Test
public void testAssertEquals_tooShort() throws Exception {
try {
executeAssertEquals("input1.txt", "input3.txt");
fail();
}
catch (AssertionError e) {
assertTrue(e.getMessage().startsWith("Line number 4 does not match."));
}
void testAssertEquals_tooShort() {
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("input1.txt", "input3.txt"));
assertTrue(error.getMessage().startsWith("Line number 4 does not match."));
}

@Test
public void testAssertEquals_blank_equal() throws Exception {
executeAssertEquals("blank.txt", "blank.txt");
void testAssertEquals_blank_equal() {
assertDoesNotThrow(() -> executeAssertEquals("blank.txt", "blank.txt"));
}

@Test
public void testAssertEquals_blank_tooLong() throws Exception {
try {
executeAssertEquals("blank.txt", "input1.txt");
fail();
}
catch (AssertionError e) {
assertTrue(e.getMessage().startsWith("More lines than expected. There should not be a line number 1."));
}
void testAssertEquals_blank_tooLong() {
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("blank.txt", "input1.txt"));
assertTrue(error.getMessage().startsWith("More lines than expected. There should not be a line number 1."));
}

@Test
public void testAssertEquals_blank_tooShort() throws Exception {
try {
executeAssertEquals("input1.txt", "blank.txt");
fail();
}
catch (AssertionError e) {
assertTrue(e.getMessage().startsWith("Line number 1 does not match."));
}
void testAssertEquals_blank_tooShort() {
Error error = assertThrows(AssertionError.class, () -> executeAssertEquals("input1.txt", "blank.txt"));
assertTrue(error.getMessage().startsWith("Line number 1 does not match."));
}

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

@Test
public void testAssertLineCount() throws Exception {
AssertFile.assertLineCount(5, new FileSystemResource(DIRECTORY + "input1.txt"));
void testAssertLineCount() {
assertDoesNotThrow(() -> AssertFile.assertLineCount(5, new FileSystemResource(DIRECTORY + "input1.txt")));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2009 the original author or authors.
* Copyright 2006-2022 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 @@ -16,19 +16,20 @@

package org.springframework.batch.test;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Arrays;
import java.util.Date;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;

public class ExecutionContextTestUtilsTests {
class ExecutionContextTestUtilsTests {

@Test
public void testFromJob() throws Exception {
void testFromJob() {
Date date = new Date();
JobExecution jobExecution = MetaDataInstanceFactory.createJobExecution();
jobExecution.getExecutionContext().put("foo", date);
Expand All @@ -37,7 +38,7 @@ public void testFromJob() throws Exception {
}

@Test
public void testFromStepInJob() throws Exception {
void testFromStepInJob() {
Date date = new Date();
JobExecution jobExecution = MetaDataInstanceFactory.createJobExecutionWithStepExecutions(123L,
Arrays.asList("foo", "bar"));
Expand All @@ -47,17 +48,16 @@ public void testFromStepInJob() throws Exception {
assertEquals(date, result);
}

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

@Test
public void testFromStep() throws Exception {
void testFromStep() {
Date date = new Date();
StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution();
stepExecution.getExecutionContext().put("foo", date);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2022 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 @@ -15,7 +15,7 @@
*/
package org.springframework.batch.test;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
Expand All @@ -42,17 +42,17 @@

import javax.sql.DataSource;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

/**
* @author mminella
* @author Mahmoud Ben Hassine
*/
public class JobLauncherTestUtilsTests {
class JobLauncherTestUtilsTests {

@Test
public void testStepExecutionWithJavaConfig() {
void testStepExecutionWithJavaConfig() {
ApplicationContext context = new AnnotationConfigApplicationContext(TestJobConfiguration.class);

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

@Test
public void getUniqueJobParameters_doesNotRepeatJobParameters() {
void getUniqueJobParameters_doesNotRepeatJobParameters() {
ApplicationContext context = new AnnotationConfigApplicationContext(TestJobConfiguration.class);
JobLauncherTestUtils testUtils = context.getBean(JobLauncherTestUtils.class);
Set<JobParameters> jobParametersSeen = new HashSet<>();
Expand All @@ -76,7 +76,7 @@ public void getUniqueJobParameters_doesNotRepeatJobParameters() {

@Configuration
@EnableBatchProcessing
public static class TestJobConfiguration {
static class TestJobConfiguration {

@Autowired
public JobBuilderFactory jobBuilderFactory;
Expand Down
Loading