-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sample app for https://stackoverflow.com/questions/60269753
- Loading branch information
1 parent
03df154
commit 33d528d
Showing
4 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.example</groupId> | ||
<artifactId>demo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>demo</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
<spring-batch-core.version>4.2.1.RELEASE</spring-batch-core.version> | ||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version> | ||
<junit.version>4.13</junit.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.batch</groupId> | ||
<artifactId>spring-batch-core</artifactId> | ||
<version>${spring-batch-core.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.batch</groupId> | ||
<artifactId>spring-batch-test</artifactId> | ||
<version>${spring-batch-core.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>${maven-compiler-plugin.version}</version> | ||
<configuration> | ||
<source>${java.version}</source> | ||
<target>${java.version}</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
67 changes: 67 additions & 0 deletions
67
so60269753/src/test/java/com/example/demo/TestExampleWithStepScopeTestExecutionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.example.demo; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import org.springframework.batch.core.StepExecution; | ||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.batch.core.configuration.annotation.StepScope; | ||
import org.springframework.batch.item.ExecutionContext; | ||
import org.springframework.batch.item.file.FlatFileItemReader; | ||
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; | ||
import org.springframework.batch.item.file.mapping.PassThroughLineMapper; | ||
import org.springframework.batch.test.MetaDataInstanceFactory; | ||
import org.springframework.batch.test.StepScopeTestExecutionListener; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.TestExecutionListeners; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; | ||
|
||
@RunWith(SpringRunner.class) | ||
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class}) | ||
@ContextConfiguration(classes = TestExampleWithStepScopeTestExecutionListener.MyConfiguration.class) | ||
public class TestExampleWithStepScopeTestExecutionListener { | ||
|
||
@Autowired | ||
private FlatFileItemReader<String> reader; | ||
|
||
public StepExecution getStepExecution() { | ||
StepExecution execution = MetaDataInstanceFactory.createStepExecution(); | ||
execution.getExecutionContext().putString("fileName", "data/input.txt"); | ||
return execution; | ||
} | ||
|
||
@Test | ||
public void givenMockedStep_whenReaderCalled_thenSuccess() throws Exception { | ||
reader.open(new ExecutionContext()); | ||
String item = reader.read(); | ||
Assert.assertEquals("foo", item); | ||
item = reader.read(); | ||
Assert.assertEquals("bar", item); | ||
item = reader.read(); | ||
Assert.assertNull(item); | ||
reader.close(); | ||
} | ||
|
||
@Configuration | ||
@EnableBatchProcessing | ||
static class MyConfiguration { | ||
|
||
@Bean | ||
@StepScope | ||
public FlatFileItemReader<String> myFlatFileItemReader(@Value("#{stepExecutionContext['fileName']}") String filename) { | ||
return new FlatFileItemReaderBuilder<String>() | ||
.name("myFlatFileItemReader") | ||
.resource(new ClassPathResource(filename)) | ||
.lineMapper(new PassThroughLineMapper()) | ||
.build(); | ||
} | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
so60269753/src/test/java/com/example/demo/TestExampleWithStepScopeTestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.example.demo; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import org.springframework.batch.core.StepExecution; | ||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.batch.core.configuration.annotation.StepScope; | ||
import org.springframework.batch.item.ExecutionContext; | ||
import org.springframework.batch.item.file.FlatFileItemReader; | ||
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; | ||
import org.springframework.batch.item.file.mapping.PassThroughLineMapper; | ||
import org.springframework.batch.test.MetaDataInstanceFactory; | ||
import org.springframework.batch.test.StepScopeTestUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@ContextConfiguration(classes = TestExampleWithStepScopeTestUtils.MyConfiguration.class) | ||
public class TestExampleWithStepScopeTestUtils { | ||
|
||
@Autowired | ||
private FlatFileItemReader<String> reader; | ||
|
||
@Test | ||
public void givenMockedStep_whenReaderCalled_thenSuccess() throws Exception { | ||
// given | ||
final ExecutionContext ctx = new ExecutionContext(); | ||
ctx.put("fileName", "data/input.txt"); | ||
StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution(ctx); | ||
|
||
// when | ||
List<String> items = StepScopeTestUtils.doInStepScope(stepExecution, () -> { | ||
List<String> result = new ArrayList<>(); | ||
String item; | ||
reader.open(new ExecutionContext()); | ||
while ((item = reader.read()) != null) { | ||
result.add(item); | ||
} | ||
reader.close(); | ||
return result; | ||
}); | ||
|
||
// then | ||
Assert.assertEquals(2, items.size()); | ||
Assert.assertEquals("foo", items.get(0)); | ||
Assert.assertEquals("bar", items.get(1)); | ||
} | ||
|
||
@Configuration | ||
@EnableBatchProcessing | ||
static class MyConfiguration { | ||
|
||
@Bean | ||
@StepScope | ||
public FlatFileItemReader<String> myFlatFileItemReader(@Value("#{stepExecutionContext['fileName']}") String filename) { | ||
return new FlatFileItemReaderBuilder<String>() | ||
.name("myFlatFileItemReader") | ||
.resource(new ClassPathResource(filename)) | ||
.lineMapper(new PassThroughLineMapper()) | ||
.build(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
foo | ||
bar |