Skip to content

Commit a6a53c4

Browse files
committed
Extract JDBC test infrastructure in a separate class
1 parent c8a0528 commit a6a53c4

File tree

2 files changed

+60
-27
lines changed

2 files changed

+60
-27
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2025-present 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+
*/
16+
package org.springframework.batch.core.step.item;
17+
18+
import javax.sql.DataSource;
19+
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.jdbc.core.JdbcTemplate;
23+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
24+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
25+
import org.springframework.jdbc.support.JdbcTransactionManager;
26+
27+
/**
28+
* JDBC batch infrastructure configuration for tests.
29+
*
30+
* @author Mahmoud Ben Hassine
31+
*/
32+
@Configuration
33+
public class JdbcInfrastructureConfiguration {
34+
35+
@Bean
36+
public DataSource dataSource() {
37+
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
38+
.addScript("/org/springframework/batch/core/schema-drop-h2.sql")
39+
.addScript("/org/springframework/batch/core/schema-h2.sql")
40+
.addScript("schema.sql")
41+
.generateUniqueName(true)
42+
.build();
43+
}
44+
45+
@Bean
46+
public JdbcTransactionManager transactionManager(DataSource dataSource) {
47+
return new JdbcTransactionManager(dataSource);
48+
}
49+
50+
@Bean
51+
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
52+
return new JdbcTemplate(dataSource);
53+
}
54+
55+
}

spring-batch-core/src/test/java/org/springframework/batch/core/step/item/TestConfiguration.java

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@
3232
import org.springframework.beans.factory.annotation.Value;
3333
import org.springframework.context.annotation.Bean;
3434
import org.springframework.context.annotation.Configuration;
35+
import org.springframework.context.annotation.Import;
3536
import org.springframework.core.io.Resource;
36-
import org.springframework.jdbc.core.JdbcTemplate;
37-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
38-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
39-
import org.springframework.jdbc.support.JdbcTransactionManager;
4037

4138
@Configuration
4239
@EnableBatchProcessing
4340
@EnableJdbcJobRepository
41+
@Import(JdbcInfrastructureConfiguration.class)
4442
public class TestConfiguration {
4543

4644
@Bean
@@ -65,34 +63,14 @@ public ItemProcessor<Person, Person> itemProcessor() {
6563
}
6664

6765
@Bean
68-
public JdbcBatchItemWriter<Person> itemWriter() {
66+
public JdbcBatchItemWriter<Person> itemWriter(DataSource dataSource) {
6967
String sql = "insert into person_target (id, name) values (:id, :name)";
70-
return new JdbcBatchItemWriterBuilder<Person>().dataSource(dataSource()).sql(sql).beanMapped().build();
68+
return new JdbcBatchItemWriterBuilder<Person>().dataSource(dataSource).sql(sql).beanMapped().build();
7169
}
7270

7371
@Bean
7472
public Job job(JobRepository jobRepository, Step step) {
75-
return new JobBuilder("job", jobRepository).start(step).build();
76-
}
77-
78-
@Bean
79-
public DataSource dataSource() {
80-
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
81-
.addScript("/org/springframework/batch/core/schema-drop-h2.sql")
82-
.addScript("/org/springframework/batch/core/schema-h2.sql")
83-
.addScript("schema.sql")
84-
.generateUniqueName(true)
85-
.build();
86-
}
87-
88-
@Bean
89-
public JdbcTransactionManager transactionManager(DataSource dataSource) {
90-
return new JdbcTransactionManager(dataSource);
91-
}
92-
93-
@Bean
94-
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
95-
return new JdbcTemplate(dataSource);
73+
return new JobBuilder(jobRepository).start(step).build();
9674
}
9775

9876
}

0 commit comments

Comments
 (0)