Skip to content

Migrate Spring Batch Samples to JUnit Jupiter #4173

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
5 changes: 5 additions & 0 deletions spring-batch-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-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,15 +15,13 @@
*/
package org.springframework.batch.sample;

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

/**
* <p>
Expand All @@ -39,10 +37,9 @@
* </p>
*/

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
@SpringJUnitConfig(
locations = { "/simple-job-launcher-context.xml", "/jobs/amqp-example-job.xml", "/job-runner-context.xml" })
public class AMQPJobFunctionalTests {
class AMQPJobFunctionalTests {

@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
Expand All @@ -51,7 +48,7 @@ public class AMQPJobFunctionalTests {
private JobExplorer jobExplorer;

@Test
public void testLaunchJob() throws Exception {
void testLaunchJob() throws Exception {

jobLauncherTestUtils.launchJob();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2007 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,23 +16,20 @@

package org.springframework.batch.sample;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/beanWrapperMapperSampleJob.xml",
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/jobs/beanWrapperMapperSampleJob.xml",
"/job-runner-context.xml" })
public class BeanWrapperMapperSampleJobFunctionalTests {
class BeanWrapperMapperSampleJobFunctionalTests {

@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;

@Test
public void testJobLaunch() throws Exception {
void testJobLaunch() throws Exception {

jobLauncherTestUtils.launchJob();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2021 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 @@ -25,24 +25,21 @@
import javax.sql.DataSource;

import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;

import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.jdbc.JdbcTestUtils;

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

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/compositeItemWriterSampleJob.xml",
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/jobs/compositeItemWriterSampleJob.xml",
"/job-runner-context.xml" })
public class CompositeItemWriterSampleFunctionalTests {
class CompositeItemWriterSampleFunctionalTests {

private static final String GET_TRADES = "SELECT isin, quantity, price, customer FROM TRADE order by isin";

Expand All @@ -63,7 +60,7 @@ public void setDataSource(DataSource dataSource) {
}

@Test
public void testJobLaunch() throws Exception {
void testJobLaunch() throws Exception {
JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE");
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE");

Expand All @@ -75,7 +72,6 @@ public void testJobLaunch() throws Exception {
}

private void checkOutputTable(int before) {
@SuppressWarnings("serial")
final List<Trade> trades = new ArrayList<Trade>() {
{
add(new Trade("UK21341EAH41", 211, new BigDecimal("31.11"), "customer1"));
Expand Down Expand Up @@ -107,7 +103,6 @@ public void processRow(ResultSet rs) throws SQLException {
}

private void checkOutputFile(String fileName) throws IOException {
@SuppressWarnings("resource")
List<String> outputLines = IOUtils.readLines(new FileInputStream(fileName), "UTF-8");

String output = "";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 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,7 +16,7 @@

package org.springframework.batch.sample;

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

import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -27,23 +27,20 @@

import javax.sql.DataSource;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.jdbc.JdbcTestUtils;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
@SpringJUnitConfig(
locations = { "/simple-job-launcher-context.xml", "/jobs/customerFilterJob.xml", "/job-runner-context.xml" })
public class CustomerFilterJobFunctionalTests {
class CustomerFilterJobFunctionalTests {

private static final String GET_CUSTOMERS = "select NAME, CREDIT from CUSTOMER order by NAME";

Expand All @@ -53,7 +50,7 @@ public class CustomerFilterJobFunctionalTests {

private JdbcTemplate jdbcTemplate;

private Map<String, Double> credits = new HashMap<>();
private final Map<String, Double> credits = new HashMap<>();

@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
Expand All @@ -63,8 +60,8 @@ public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}

@Before
public void onSetUp() throws Exception {
@BeforeEach
void onSetUp() {
JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE");
JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "CUSTOMER", "ID > 4");
jdbcTemplate.update("update CUSTOMER set credit=100000");
Expand All @@ -76,14 +73,14 @@ public void onSetUp() throws Exception {
}
}

@After
public void tearDown() throws Exception {
@AfterEach
void tearDown() {
JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE");
JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "CUSTOMER", "ID > 4");
}

@Test
public void testFilterJob() throws Exception {
void testFilterJob() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils.launchJob();

customers = Arrays.asList(new Customer("customer1", (credits.get("customer1"))),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 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,22 +16,20 @@

package org.springframework.batch.sample;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.launch.JobOperator;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

/**
* Functional test for graceful shutdown. A batch container is started in a new thread,
Expand All @@ -41,10 +39,9 @@
* @author Mahmoud Ben Hassine
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
@SpringJUnitConfig(
locations = { "/simple-job-launcher-context.xml", "/jobs/infiniteLoopJob.xml", "/job-runner-context.xml" })
public class DatabaseShutdownFunctionalTests {
class DatabaseShutdownFunctionalTests {

/** Logger */
protected final Log logger = LogFactory.getLog(getClass());
Expand All @@ -56,7 +53,7 @@ public class DatabaseShutdownFunctionalTests {
private JobLauncherTestUtils jobLauncherTestUtils;

@Test
public void testLaunchJob() throws Exception {
void testLaunchJob() throws Exception {

JobExecution jobExecution = jobLauncherTestUtils.launchJob();

Expand All @@ -75,7 +72,7 @@ public void testLaunchJob() throws Exception {
count++;
}

assertFalse("Timed out waiting for job to end.", jobExecution.isRunning());
assertFalse(jobExecution.isRunning(), "Timed out waiting for job to end.");
assertEquals(BatchStatus.STOPPED, jobExecution.getStatus());

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2007-2009 the original author or authors.
* Copyright 2007-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,21 +15,18 @@
*/
package org.springframework.batch.sample;

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.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.batch.sample.domain.person.PersonService;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
@SpringJUnitConfig(
locations = { "/simple-job-launcher-context.xml", "/jobs/delegatingJob.xml", "/job-runner-context.xml" })
public class DelegatingJobFunctionalTests {
class DelegatingJobFunctionalTests {

@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
Expand All @@ -38,7 +35,7 @@ public class DelegatingJobFunctionalTests {
private PersonService personService;

@Test
public void testLaunchJob() throws Exception {
void testLaunchJob() throws Exception {

jobLauncherTestUtils.launchJob();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2007-2021 the original author or authors.
* Copyright 2007-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,23 +15,20 @@
*/
package org.springframework.batch.sample;

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

import javax.sql.DataSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.jdbc.JdbcTestUtils;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
@SpringJUnitConfig(
locations = { "/simple-job-launcher-context.xml", "/jobs/footballJob.xml", "/job-runner-context.xml" })
public class FootballJobFunctionalTests {
class FootballJobFunctionalTests {

@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
Expand All @@ -44,7 +41,7 @@ public void setDataSource(DataSource dataSource) {
}

@Test
public void testLaunchJob() throws Exception {
void testLaunchJob() throws Exception {
JdbcTestUtils.deleteFromTables(jdbcTemplate, "PLAYERS", "GAMES", "PLAYER_SUMMARY");

jobLauncherTestUtils.launchJob();
Expand Down
Loading