Skip to content

Migrate Spring Batch Integration to JUnit Jupiter #4124

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
12 changes: 3 additions & 9 deletions spring-batch-integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,10 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.integration;

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.beans.factory.annotation.Autowired;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class SmokeTests {
@SpringJUnitConfig
class SmokeTests {

@Autowired
private MessageChannel smokein;
Expand All @@ -26,12 +38,12 @@ public class SmokeTests {
private PollableChannel smokeout;

@Test
public void testDummyWithSimpleAssert() throws Exception {
void testDummyWithSimpleAssert() {
assertTrue(true);
}

@Test
public void testVanillaSendAndReceive() throws Exception {
void testVanillaSendAndReceive() {
smokein.send(new GenericMessage<>("foo"));
@SuppressWarnings("unchecked")
Message<String> message = (Message<String>) smokeout.receive(100);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2020 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 @@ -15,73 +15,43 @@
*/
package org.springframework.batch.integration.async;

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

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
import org.junit.runner.RunWith;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.test.MetaDataInstanceFactory;
import org.springframework.batch.test.StepScopeTestUtils;
import org.springframework.batch.test.StepScopeTestExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.TestExecutionListeners.MergeMode;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class AsyncItemProcessorMessagingGatewayTests {
@SpringJUnitConfig
@TestExecutionListeners(listeners = StepScopeTestExecutionListener.class, mergeMode = MergeMode.MERGE_WITH_DEFAULTS)
class AsyncItemProcessorMessagingGatewayTests {

private final AsyncItemProcessor<String, String> processor = new AsyncItemProcessor<>();

private final StepExecution stepExecution = MetaDataInstanceFactory
.createStepExecution(new JobParametersBuilder().addLong("factor", 2L).toJobParameters());

;

@Rule
public MethodRule rule = new MethodRule() {
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
StepScopeTestUtils.doInStepScope(stepExecution, new Callable<Void>() {
public Void call() throws Exception {
try {
base.evaluate();
}
catch (Exception e) {
throw e;
}
catch (Throwable e) {
throw new Error(e);
}
return null;
}
});
};
};
}
};

@Autowired
private ItemProcessor<String, String> delegate;

StepExecution getStepExecution() {
return MetaDataInstanceFactory
.createStepExecution(new JobParametersBuilder().addLong("factor", 2L).toJobParameters());
}

@Test
public void testMultiExecution() throws Exception {
void testMultiExecution() throws Exception {
processor.setDelegate(delegate);
processor.setTaskExecutor(new SimpleAsyncTaskExecutor());
List<Future<String>> list = new ArrayList<>();
Expand All @@ -102,7 +72,7 @@ public void testMultiExecution() throws Exception {
}

@MessageEndpoint
public static class Doubler {
static class Doubler {

private int factor = 1;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2019 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 @@ -15,15 +15,16 @@
*/
package org.springframework.batch.integration.async;

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 java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.scope.context.StepContext;
import org.springframework.batch.core.scope.context.StepSynchronizationManager;
import org.springframework.batch.item.ItemProcessor;
Expand All @@ -32,38 +33,38 @@
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.lang.Nullable;

public class AsyncItemProcessorTests {
class AsyncItemProcessorTests {

private AsyncItemProcessor<String, String> processor = new AsyncItemProcessor<>();
private final AsyncItemProcessor<String, String> processor = new AsyncItemProcessor<>();

private ItemProcessor<String, String> delegate = new ItemProcessor<String, String>() {
@Nullable
public String process(String item) throws Exception {
return item + item;
};
}
};

@Test(expected = IllegalArgumentException.class)
public void testNoDelegate() throws Exception {
processor.afterPropertiesSet();
@Test
void testNoDelegate() {
assertThrows(IllegalArgumentException.class, processor::afterPropertiesSet);
}

@Test
public void testExecution() throws Exception {
void testExecution() throws Exception {
processor.setDelegate(delegate);
Future<String> result = processor.process("foo");
assertEquals("foofoo", result.get());
}

@Test
public void testExecutionInStepScope() throws Exception {
void testExecutionInStepScope() throws Exception {
delegate = new ItemProcessor<String, String>() {
@Nullable
public String process(String item) throws Exception {
StepContext context = StepSynchronizationManager.getContext();
assertTrue(context != null && context.getStepExecution() != null);
return item + item;
};
}
};
processor.setDelegate(delegate);
Future<String> result = StepScopeTestUtils.doInStepScope(MetaDataInstanceFactory.createStepExecution(),
Expand All @@ -76,7 +77,7 @@ public Future<String> call() throws Exception {
}

@Test
public void testMultiExecution() throws Exception {
void testMultiExecution() throws Exception {
processor.setDelegate(delegate);
processor.setTaskExecutor(new SimpleAsyncTaskExecutor());
List<Future<String>> list = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 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 @@ -24,8 +24,8 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

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

import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStreamException;
Expand All @@ -34,30 +34,31 @@
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author mminella
*/
public class AsyncItemWriterTests {
class AsyncItemWriterTests {

private AsyncItemWriter<String> writer;

private List<String> writtenItems;

private TaskExecutor taskExecutor;

@Before
public void setup() {
@BeforeEach
void setup() {
taskExecutor = new SimpleAsyncTaskExecutor();
writtenItems = new ArrayList<>();
writer = new AsyncItemWriter<>();
}

@Test
public void testRoseyScenario() throws Exception {
void testRoseyScenario() throws Exception {
writer.setDelegate(new ListItemWriter(writtenItems));
List<FutureTask<String>> processedItems = new ArrayList<>();

Expand Down Expand Up @@ -87,7 +88,7 @@ public String call() throws Exception {
}

@Test
public void testFilteredItem() throws Exception {
void testFilteredItem() throws Exception {
writer.setDelegate(new ListItemWriter(writtenItems));
List<FutureTask<String>> processedItems = new ArrayList<>();

Expand Down Expand Up @@ -116,7 +117,7 @@ public String call() throws Exception {
}

@Test
public void testException() throws Exception {
void testException() {
writer.setDelegate(new ListItemWriter(writtenItems));
List<FutureTask<String>> processedItems = new ArrayList<>();

Expand All @@ -138,17 +139,12 @@ public String call() throws Exception {
taskExecutor.execute(processedItem);
}

try {
writer.write(processedItems);
}
catch (Exception e) {
assertTrue(e instanceof RuntimeException);
assertEquals("This was expected", e.getMessage());
}
Exception exception = assertThrows(RuntimeException.class, () -> writer.write(processedItems));
assertEquals("This was expected", exception.getMessage());
}

@Test
public void testExecutionException() {
void testExecutionException() {
ListItemWriter delegate = new ListItemWriter(writtenItems);
writer.setDelegate(delegate);
List<Future<String>> processedItems = new ArrayList<>();
Expand Down Expand Up @@ -182,18 +178,14 @@ public String get(long timeout, TimeUnit unit)
}
});

try {
writer.write(processedItems);
}
catch (Exception e) {
assertFalse(e instanceof ExecutionException);
}
Exception exception = assertThrows(Exception.class, () -> writer.write(processedItems));
assertFalse(exception instanceof ExecutionException);

assertEquals(0, writtenItems.size());
}

@Test
public void testStreamDelegate() throws Exception {
void testStreamDelegate() throws Exception {
ListItemStreamWriter itemWriter = new ListItemStreamWriter(writtenItems);
writer.setDelegate(itemWriter);

Expand All @@ -211,7 +203,7 @@ public void testStreamDelegate() throws Exception {
}

@Test
public void testNonStreamDelegate() throws Exception {
void testNonStreamDelegate() throws Exception {
ListItemWriter itemWriter = new ListItemWriter(writtenItems);
writer.setDelegate(itemWriter);

Expand Down
Loading