Skip to content

Commit ae5ffd2

Browse files
hpoettkerfmbenhassine
authored andcommitted
Migrate Spring Batch Integration to JUnit Jupiter
1 parent e4056b4 commit ae5ffd2

File tree

40 files changed

+886
-889
lines changed

40 files changed

+886
-889
lines changed

spring-batch-integration/pom.xml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,10 @@
111111
<scope>test</scope>
112112
</dependency>
113113
<dependency>
114-
<groupId>junit</groupId>
115-
<artifactId>junit</artifactId>
116-
<version>${junit.version}</version>
114+
<groupId>org.junit.jupiter</groupId>
115+
<artifactId>junit-jupiter-engine</artifactId>
116+
<version>${junit-jupiter.version}</version>
117117
<scope>test</scope>
118-
<exclusions>
119-
<exclusion>
120-
<groupId>org.hamcrest</groupId>
121-
<artifactId>hamcrest-core</artifactId>
122-
</exclusion>
123-
</exclusions>
124118
</dependency>
125119
<dependency>
126120
<groupId>org.slf4j</groupId>

spring-batch-integration/src/test/java/org/springframework/batch/integration/SmokeTests.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
1+
/*
2+
* Copyright 2008-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.batch.integration;
217

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertTrue;
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
520

6-
import org.junit.Test;
7-
import org.junit.runner.RunWith;
21+
import org.junit.jupiter.api.Test;
822
import org.springframework.beans.factory.annotation.Autowired;
923
import org.springframework.integration.annotation.MessageEndpoint;
1024
import org.springframework.integration.annotation.ServiceActivator;
1125
import org.springframework.messaging.Message;
1226
import org.springframework.messaging.MessageChannel;
1327
import org.springframework.messaging.PollableChannel;
1428
import org.springframework.messaging.support.GenericMessage;
15-
import org.springframework.test.context.ContextConfiguration;
16-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
29+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
1730

18-
@ContextConfiguration
19-
@RunWith(SpringJUnit4ClassRunner.class)
20-
public class SmokeTests {
31+
@SpringJUnitConfig
32+
class SmokeTests {
2133

2234
@Autowired
2335
private MessageChannel smokein;
@@ -26,12 +38,12 @@ public class SmokeTests {
2638
private PollableChannel smokeout;
2739

2840
@Test
29-
public void testDummyWithSimpleAssert() throws Exception {
41+
void testDummyWithSimpleAssert() {
3042
assertTrue(true);
3143
}
3244

3345
@Test
34-
public void testVanillaSendAndReceive() throws Exception {
46+
void testVanillaSendAndReceive() {
3547
smokein.send(new GenericMessage<>("foo"));
3648
@SuppressWarnings("unchecked")
3749
Message<String> message = (Message<String>) smokeout.receive(100);

spring-batch-integration/src/test/java/org/springframework/batch/integration/async/AsyncItemProcessorMessagingGatewayTests.java

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2020 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.
@@ -15,73 +15,43 @@
1515
*/
1616
package org.springframework.batch.integration.async;
1717

18-
import static org.junit.Assert.assertNotNull;
19-
import static org.junit.Assert.assertTrue;
18+
import static org.junit.jupiter.api.Assertions.assertNotNull;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
2020

2121
import java.util.ArrayList;
2222
import java.util.List;
23-
import java.util.concurrent.Callable;
2423
import java.util.concurrent.Future;
2524

26-
import org.junit.Rule;
27-
import org.junit.Test;
28-
import org.junit.rules.MethodRule;
29-
import org.junit.runner.RunWith;
30-
import org.junit.runners.model.FrameworkMethod;
31-
import org.junit.runners.model.Statement;
25+
import org.junit.jupiter.api.Test;
3226
import org.springframework.batch.core.JobParametersBuilder;
3327
import org.springframework.batch.core.StepExecution;
3428
import org.springframework.batch.item.ItemProcessor;
3529
import org.springframework.batch.test.MetaDataInstanceFactory;
36-
import org.springframework.batch.test.StepScopeTestUtils;
30+
import org.springframework.batch.test.StepScopeTestExecutionListener;
3731
import org.springframework.beans.factory.annotation.Autowired;
3832
import org.springframework.core.task.SimpleAsyncTaskExecutor;
3933
import org.springframework.integration.annotation.MessageEndpoint;
4034
import org.springframework.integration.annotation.ServiceActivator;
41-
import org.springframework.test.context.ContextConfiguration;
42-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
35+
import org.springframework.test.context.TestExecutionListeners;
36+
import org.springframework.test.context.TestExecutionListeners.MergeMode;
37+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
4338

44-
@RunWith(SpringJUnit4ClassRunner.class)
45-
@ContextConfiguration
46-
public class AsyncItemProcessorMessagingGatewayTests {
39+
@SpringJUnitConfig
40+
@TestExecutionListeners(listeners = StepScopeTestExecutionListener.class, mergeMode = MergeMode.MERGE_WITH_DEFAULTS)
41+
class AsyncItemProcessorMessagingGatewayTests {
4742

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

50-
private final StepExecution stepExecution = MetaDataInstanceFactory
51-
.createStepExecution(new JobParametersBuilder().addLong("factor", 2L).toJobParameters());
52-
53-
;
54-
55-
@Rule
56-
public MethodRule rule = new MethodRule() {
57-
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
58-
return new Statement() {
59-
@Override
60-
public void evaluate() throws Throwable {
61-
StepScopeTestUtils.doInStepScope(stepExecution, new Callable<Void>() {
62-
public Void call() throws Exception {
63-
try {
64-
base.evaluate();
65-
}
66-
catch (Exception e) {
67-
throw e;
68-
}
69-
catch (Throwable e) {
70-
throw new Error(e);
71-
}
72-
return null;
73-
}
74-
});
75-
};
76-
};
77-
}
78-
};
79-
8045
@Autowired
8146
private ItemProcessor<String, String> delegate;
8247

48+
StepExecution getStepExecution() {
49+
return MetaDataInstanceFactory
50+
.createStepExecution(new JobParametersBuilder().addLong("factor", 2L).toJobParameters());
51+
}
52+
8353
@Test
84-
public void testMultiExecution() throws Exception {
54+
void testMultiExecution() throws Exception {
8555
processor.setDelegate(delegate);
8656
processor.setTaskExecutor(new SimpleAsyncTaskExecutor());
8757
List<Future<String>> list = new ArrayList<>();
@@ -102,7 +72,7 @@ public void testMultiExecution() throws Exception {
10272
}
10373

10474
@MessageEndpoint
105-
public static class Doubler {
75+
static class Doubler {
10676

10777
private int factor = 1;
10878

spring-batch-integration/src/test/java/org/springframework/batch/integration/async/AsyncItemProcessorTests.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2019 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.
@@ -15,15 +15,16 @@
1515
*/
1616
package org.springframework.batch.integration.async;
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

2122
import java.util.ArrayList;
2223
import java.util.List;
2324
import java.util.concurrent.Callable;
2425
import java.util.concurrent.Future;
2526

26-
import org.junit.Test;
27+
import org.junit.jupiter.api.Test;
2728
import org.springframework.batch.core.scope.context.StepContext;
2829
import org.springframework.batch.core.scope.context.StepSynchronizationManager;
2930
import org.springframework.batch.item.ItemProcessor;
@@ -32,38 +33,38 @@
3233
import org.springframework.core.task.SimpleAsyncTaskExecutor;
3334
import org.springframework.lang.Nullable;
3435

35-
public class AsyncItemProcessorTests {
36+
class AsyncItemProcessorTests {
3637

37-
private AsyncItemProcessor<String, String> processor = new AsyncItemProcessor<>();
38+
private final AsyncItemProcessor<String, String> processor = new AsyncItemProcessor<>();
3839

3940
private ItemProcessor<String, String> delegate = new ItemProcessor<String, String>() {
4041
@Nullable
4142
public String process(String item) throws Exception {
4243
return item + item;
43-
};
44+
}
4445
};
4546

46-
@Test(expected = IllegalArgumentException.class)
47-
public void testNoDelegate() throws Exception {
48-
processor.afterPropertiesSet();
47+
@Test
48+
void testNoDelegate() {
49+
assertThrows(IllegalArgumentException.class, processor::afterPropertiesSet);
4950
}
5051

5152
@Test
52-
public void testExecution() throws Exception {
53+
void testExecution() throws Exception {
5354
processor.setDelegate(delegate);
5455
Future<String> result = processor.process("foo");
5556
assertEquals("foofoo", result.get());
5657
}
5758

5859
@Test
59-
public void testExecutionInStepScope() throws Exception {
60+
void testExecutionInStepScope() throws Exception {
6061
delegate = new ItemProcessor<String, String>() {
6162
@Nullable
6263
public String process(String item) throws Exception {
6364
StepContext context = StepSynchronizationManager.getContext();
6465
assertTrue(context != null && context.getStepExecution() != null);
6566
return item + item;
66-
};
67+
}
6768
};
6869
processor.setDelegate(delegate);
6970
Future<String> result = StepScopeTestUtils.doInStepScope(MetaDataInstanceFactory.createStepExecution(),
@@ -76,7 +77,7 @@ public Future<String> call() throws Exception {
7677
}
7778

7879
@Test
79-
public void testMultiExecution() throws Exception {
80+
void testMultiExecution() throws Exception {
8081
processor.setDelegate(delegate);
8182
processor.setTaskExecutor(new SimpleAsyncTaskExecutor());
8283
List<Future<String>> list = new ArrayList<>();

spring-batch-integration/src/test/java/org/springframework/batch/integration/async/AsyncItemWriterTests.java

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 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.
@@ -24,8 +24,8 @@
2424
import java.util.concurrent.TimeUnit;
2525
import java.util.concurrent.TimeoutException;
2626

27-
import org.junit.Before;
28-
import org.junit.Test;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
2929

3030
import org.springframework.batch.item.ExecutionContext;
3131
import org.springframework.batch.item.ItemStreamException;
@@ -34,30 +34,31 @@
3434
import org.springframework.core.task.SimpleAsyncTaskExecutor;
3535
import org.springframework.core.task.TaskExecutor;
3636

37-
import static org.junit.Assert.assertEquals;
38-
import static org.junit.Assert.assertFalse;
39-
import static org.junit.Assert.assertTrue;
37+
import static org.junit.jupiter.api.Assertions.assertEquals;
38+
import static org.junit.jupiter.api.Assertions.assertFalse;
39+
import static org.junit.jupiter.api.Assertions.assertThrows;
40+
import static org.junit.jupiter.api.Assertions.assertTrue;
4041

4142
/**
4243
* @author mminella
4344
*/
44-
public class AsyncItemWriterTests {
45+
class AsyncItemWriterTests {
4546

4647
private AsyncItemWriter<String> writer;
4748

4849
private List<String> writtenItems;
4950

5051
private TaskExecutor taskExecutor;
5152

52-
@Before
53-
public void setup() {
53+
@BeforeEach
54+
void setup() {
5455
taskExecutor = new SimpleAsyncTaskExecutor();
5556
writtenItems = new ArrayList<>();
5657
writer = new AsyncItemWriter<>();
5758
}
5859

5960
@Test
60-
public void testRoseyScenario() throws Exception {
61+
void testRoseyScenario() throws Exception {
6162
writer.setDelegate(new ListItemWriter(writtenItems));
6263
List<FutureTask<String>> processedItems = new ArrayList<>();
6364

@@ -87,7 +88,7 @@ public String call() throws Exception {
8788
}
8889

8990
@Test
90-
public void testFilteredItem() throws Exception {
91+
void testFilteredItem() throws Exception {
9192
writer.setDelegate(new ListItemWriter(writtenItems));
9293
List<FutureTask<String>> processedItems = new ArrayList<>();
9394

@@ -116,7 +117,7 @@ public String call() throws Exception {
116117
}
117118

118119
@Test
119-
public void testException() throws Exception {
120+
void testException() {
120121
writer.setDelegate(new ListItemWriter(writtenItems));
121122
List<FutureTask<String>> processedItems = new ArrayList<>();
122123

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

141-
try {
142-
writer.write(processedItems);
143-
}
144-
catch (Exception e) {
145-
assertTrue(e instanceof RuntimeException);
146-
assertEquals("This was expected", e.getMessage());
147-
}
142+
Exception exception = assertThrows(RuntimeException.class, () -> writer.write(processedItems));
143+
assertEquals("This was expected", exception.getMessage());
148144
}
149145

150146
@Test
151-
public void testExecutionException() {
147+
void testExecutionException() {
152148
ListItemWriter delegate = new ListItemWriter(writtenItems);
153149
writer.setDelegate(delegate);
154150
List<Future<String>> processedItems = new ArrayList<>();
@@ -182,18 +178,14 @@ public String get(long timeout, TimeUnit unit)
182178
}
183179
});
184180

185-
try {
186-
writer.write(processedItems);
187-
}
188-
catch (Exception e) {
189-
assertFalse(e instanceof ExecutionException);
190-
}
181+
Exception exception = assertThrows(Exception.class, () -> writer.write(processedItems));
182+
assertFalse(exception instanceof ExecutionException);
191183

192184
assertEquals(0, writtenItems.size());
193185
}
194186

195187
@Test
196-
public void testStreamDelegate() throws Exception {
188+
void testStreamDelegate() throws Exception {
197189
ListItemStreamWriter itemWriter = new ListItemStreamWriter(writtenItems);
198190
writer.setDelegate(itemWriter);
199191

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

213205
@Test
214-
public void testNonStreamDelegate() throws Exception {
206+
void testNonStreamDelegate() throws Exception {
215207
ListItemWriter itemWriter = new ListItemWriter(writtenItems);
216208
writer.setDelegate(itemWriter);
217209

0 commit comments

Comments
 (0)