25
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Spring BatchでのUnitTest

Last updated at Posted at 2014-05-07

できること

Spring Batchの各Stepを単独で実行できる。もちろんJobの実行も。

手順

  1. まずは普通にUnitTestクラスを作る。

  2. @RunWithの指定に、SpringJUnit4ClassRunnerを設定。
    (mavenを使ってる場合は、dependencyにspring-testが必要)

  3. @ContextConfigurationで、SpringBatch定義と、テスト用のJobLauncherTestUtilsの設定ファイルを指定。(mavenを使っている場合は、dependencyにspring-batch-testが必要)

  4. UnitTestクラスに@AutowiredでjobLauncherTestUtilsをDIさせる。

サンプルコード

test-context.xml
	<bean id="jobLauncherTestUtils" class="org.springframework.batch.test.JobLauncherTestUtils">
		<property name="job" ref="sampleJob" />
		<property name="jobRepository" ref="jobRepository" />
		<property name="jobLauncher" ref="jobLauncher" />
	</bean>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/batch-context.xml", "/test-context.xml" })
public class SampleTest {

	@Autowired
	private JobLauncherTestUtils jobLauncherTestUtils;

	@Test
	public void testStep() throws Exception {
		// 単独のStepの実行
		jobLauncherTestUtils.launchStep("sampleStep");
	}

	@Test
	public void testJob() throws Exception {
		// Jobの実行 もちろんパラメータ設定もできる
		jobLauncherTestUtils.launchJob(new JobParameters());
	}
}
25
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?