単体テスト作成
`# 単体テスト作成
事前準備
下記設定を追加します:
test/resources/application-test.properties
# エンティティクラスからスキーマを自動生成しない
spring.jpa.hibernate.ddl-auto=none
# h2db接続設定
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# 起動時に自動でジョブが実行されるのを無効化
spring.batch.job.enabled=false
pom.xml
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
XxxApplicationTests.java
// test配下の自動生成済みテストクラスに下記を追記
@ActiveProfiles("test")
作成手順
テストデータ作成
test/resources/sql/customerregist配下にテストデータ投入向けのSQLを用意します:
schema.sql
CREATE TABLE sign_up (
last_name VARCHAR(24) NOT NULL
, first_name VARCHAR(24) NOT NULL
, age INT NOT NULL
);
CREATE TABLE customer (
id INT PRIMARY KEY AUTO_INCREMENT
, full_name VARCHAR(48) NOT NULL
, age INT NOT NULL
);
-- メタテーブル作成
-- https://spring.pleiades.io/spring-batch/reference/schema-appendix.html を参考にMariaDB向けに少し弄っています。
CREATE SEQUENCE BATCH_STEP_EXECUTION_SEQ;
CREATE SEQUENCE BATCH_JOB_EXECUTION_SEQ;
CREATE SEQUENCE BATCH_JOB_SEQ;
CREATE TABLE BATCH_JOB_INSTANCE (
JOB_INSTANCE_ID BIGINT PRIMARY KEY ,
VERSION BIGINT,
JOB_NAME VARCHAR(100) NOT NULL ,
JOB_KEY VARCHAR(32) NOT NULL
);
CREATE TABLE BATCH_JOB_EXECUTION (
JOB_EXECUTION_ID BIGINT PRIMARY KEY ,
VERSION BIGINT,
JOB_INSTANCE_ID BIGINT NOT NULL,
CREATE_TIME TIMESTAMP NOT NULL,
START_TIME TIMESTAMP DEFAULT NULL,
END_TIME TIMESTAMP DEFAULT NULL,
STATUS VARCHAR(10),
EXIT_CODE VARCHAR(20),
EXIT_MESSAGE VARCHAR(2500),
LAST_UPDATED TIMESTAMP,
constraint JOB_INSTANCE_EXECUTION_FK foreign key (JOB_INSTANCE_ID)
references BATCH_JOB_INSTANCE(JOB_INSTANCE_ID)
);
CREATE TABLE BATCH_JOB_EXECUTION_PARAMS (
JOB_EXECUTION_ID BIGINT NOT NULL ,
PARAMETER_NAME VARCHAR(100) NOT NULL ,
PARAMETER_TYPE VARCHAR(100) NOT NULL ,
PARAMETER_VALUE VARCHAR(2500) ,
IDENTIFYING CHAR(1) NOT NULL ,
constraint JOB_EXEC_PARAMS_FK foreign key (JOB_EXECUTION_ID)
references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID)
);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY ,
VERSION BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
JOB_EXECUTION_ID BIGINT NOT NULL,
CREATE_TIME TIMESTAMP NOT NULL,
START_TIME TIMESTAMP DEFAULT NULL ,
END_TIME TIMESTAMP DEFAULT NULL,
STATUS VARCHAR(10),
COMMIT_COUNT BIGINT ,
READ_COUNT BIGINT ,
FILTER_COUNT BIGINT ,
WRITE_COUNT BIGINT ,
READ_SKIP_COUNT BIGINT ,
WRITE_SKIP_COUNT BIGINT ,
PROCESS_SKIP_COUNT BIGINT ,
ROLLBACK_COUNT BIGINT ,
EXIT_CODE VARCHAR(20) ,
EXIT_MESSAGE VARCHAR(2500) ,
LAST_UPDATED TIMESTAMP,
constraint JOB_EXECUTION_STEP_FK foreign key (JOB_EXECUTION_ID)
references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID)
);
CREATE TABLE BATCH_JOB_EXECUTION_CONTEXT (
JOB_EXECUTION_ID BIGINT PRIMARY KEY,
SHORT_CONTEXT VARCHAR(2500) NOT NULL,
SERIALIZED_CONTEXT TEXT,
constraint JOB_EXEC_CTX_FK foreign key (JOB_EXECUTION_ID)
references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID)
);
CREATE TABLE BATCH_STEP_EXECUTION_CONTEXT (
STEP_EXECUTION_ID BIGINT PRIMARY KEY,
SHORT_CONTEXT VARCHAR(2500) NOT NULL,
SERIALIZED_CONTEXT TEXT,
constraint STEP_EXEC_CTX_FK foreign key (STEP_EXECUTION_ID)
references BATCH_STEP_EXECUTION(STEP_EXECUTION_ID)
);
data.sql
INSERT INTO sign_up (
last_name
, first_name
, age
) VALUES (
'snail'
, 'nob01'
, 13
), (
'snail'
, 'nob02'
, 14
);
テストケース作成
sign_upテーブルからデータを取得し、last_name, family_nameを結合してcustomerテーブルに登録するジョブのテストです。
job/CustomerRegistTest.java
package nob.example.easybatch.job;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.job.Job;
import org.springframework.batch.core.job.JobExecution;
import org.springframework.batch.test.JobOperatorTestUtils;
import org.springframework.batch.test.context.SpringBatchTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import nob.example.easybatch.entity.Customer;
/**
* 顧客登録ジョブのテストクラスです。
*
* @author nob
*/
@SpringBatchTest
@SpringBootTest
@ActiveProfiles("test")
@TestPropertySource(properties = {
"spring.sql.init.schema-locations=classpath:/sql/customerregist/schema.sql",
"spring.sql.init.data-locations=classpath:/sql/customerregist/data.sql"
})
public class CustomerRegistTest {
@Autowired
private Job customerRegist;
@Autowired
private JobOperatorTestUtils jobOperatorTestUtils;
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void test() throws Exception {
try {
// ジョブ実行
jobOperatorTestUtils.setJob(customerRegist);
JobExecution jobExecution = jobOperatorTestUtils.startJob();
// バッチが終了していることを確認
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
// 登録内容の確認
List<Customer> customerList = jdbcTemplate.query("SELECT id, full_name, age FROM customer",
new RowMapper<Customer>() {
@Override
public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Customer(rs.getInt("id"), rs.getString("full_name"), rs.getInt("age"));
}
});
assertEquals(2, customerList.size());
assertEquals(1, customerList.get(0).getId());
assertEquals("snailnob01", customerList.get(0).getFullName());
assertEquals(13, customerList.get(0).getAge());
assertEquals(2, customerList.get(1).getId());
assertEquals("snailnob02", customerList.get(1).getFullName());
assertEquals(14, customerList.get(1).getAge());
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
}
テスト起動
./mvnw test -Dtest=CustomerRegistTest