Skip to content

Commit 6b1cc99

Browse files
committed
Count the number of rows processed, and better names
1 parent 14bcfa8 commit 6b1cc99

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

src/test/java/examples/springbatch/BatchConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ public Step step1(ItemReader<Person> reader, ItemProcessor<Person, Person> proce
128128
}
129129

130130
@Bean
131-
public Job importUserJob(Step step1) {
132-
return jobBuilderFactory.get("importUserJob")
131+
public Job upperCaseLastName(Step step1) {
132+
return jobBuilderFactory.get("upperCaseLastName")
133133
.incrementer(new RunIdIncrementer())
134134
.flow(step1)
135135
.end()

src/test/java/examples/springbatch/PersonProcessor.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,33 @@
1515
*/
1616
package examples.springbatch;
1717

18+
import org.springframework.batch.core.StepExecution;
19+
import org.springframework.batch.core.annotation.BeforeStep;
20+
import org.springframework.batch.item.ExecutionContext;
1821
import org.springframework.batch.item.ItemProcessor;
1922

2023
public class PersonProcessor implements ItemProcessor<Person, Person> {
24+
25+
private ExecutionContext executionContext;
2126

2227
@Override
2328
public Person process(Person person) throws Exception {
29+
incrementRowCount();
30+
2431
Person transformed = new Person();
2532
transformed.setId(person.getId());
2633
transformed.setFirstName(person.getFirstName().toUpperCase());
2734
transformed.setLastName(person.getLastName().toUpperCase());
2835
return transformed;
2936
}
37+
38+
@BeforeStep
39+
public void beforeStep(StepExecution stepExecution) {
40+
executionContext = stepExecution.getExecutionContext();
41+
}
42+
43+
private void incrementRowCount() {
44+
executionContext.putInt("row_count",
45+
executionContext.getInt("row_count", 0) + 1);
46+
}
3047
}

src/test/java/examples/springbatch/SpringBatchTest.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import org.mybatis.dynamic.sql.select.SelectDSL;
2727
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
2828
import org.mybatis.spring.SqlSessionFactoryBean;
29+
import org.springframework.batch.core.ExitStatus;
30+
import org.springframework.batch.core.JobExecution;
31+
import org.springframework.batch.core.StepExecution;
2932
import org.springframework.batch.test.JobLauncherTestUtils;
3033
import org.springframework.beans.factory.annotation.Autowired;
3134
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -40,15 +43,26 @@ public class SpringBatchTest {
4043
private SqlSessionFactoryBean sqlSessionFactory;
4144

4245
@Test
43-
public void testJob() throws Exception {
44-
assertThat(getCount()).isEqualTo(0);
46+
public void testThatRowsAreTransformedToUpperCase() throws Exception {
47+
// starting condition
48+
assertThat(upperCaseRowCount()).isEqualTo(0);
4549

46-
jobLauncherTestUtils.launchJob();
50+
JobExecution execution = jobLauncherTestUtils.launchJob();
51+
assertThat(execution.getExitStatus()).isEqualTo(ExitStatus.COMPLETED);
52+
assertThat(numberOfRowsProcessed(execution)).isEqualTo(2);
4753

48-
assertThat(getCount()).isEqualTo(2);
54+
// ending condition
55+
assertThat(upperCaseRowCount()).isEqualTo(2);
4956
}
5057

51-
private long getCount() throws Exception {
58+
private int numberOfRowsProcessed(JobExecution jobExecution) {
59+
return jobExecution.getStepExecutions().stream()
60+
.map(StepExecution::getExecutionContext)
61+
.mapToInt(ec -> ec.getInt("row_count"))
62+
.sum();
63+
}
64+
65+
private long upperCaseRowCount() throws Exception {
5266
try (SqlSession sqlSession = sqlSessionFactory.getObject().openSession()) {
5367
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
5468

0 commit comments

Comments
 (0)