-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SpringBatchTest.java
149 lines (145 loc) · 5.31 KB
/
SpringBatchTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright 2018-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.test.context;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.batch.test.JobRepositoryTestUtils;
import org.springframework.batch.test.JobScopeTestExecutionListener;
import org.springframework.batch.test.StepScopeTestExecutionListener;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit.jupiter.SpringExtension;
/**
* Annotation that can be specified on a test class that runs Spring Batch based tests.
* Provides the following features over the regular <em>Spring TestContext Framework</em>:
* <ul>
* <li>Registers a {@link JobLauncherTestUtils} bean with the
* {@link BatchTestContextCustomizer#JOB_LAUNCHER_TEST_UTILS_BEAN_NAME} which can be used
* in tests for launching jobs and steps.
* </li>
* <li>Registers a {@link JobRepositoryTestUtils} bean
* with the {@link BatchTestContextCustomizer#JOB_REPOSITORY_TEST_UTILS_BEAN_NAME}
* which can be used in tests setup to create or remove job executions.
* </li>
* <li>Registers the {@link StepScopeTestExecutionListener} and {@link JobScopeTestExecutionListener}
* as test execution listeners which are required to test step/job scoped beans.
* </li>
* </ul>
* <p>
* A typical usage of this annotation with JUnit 4 is like:
*
* <pre class="code">
* @RunWith(SpringRunner.class)
* @SpringBatchTest
* @ContextConfiguration(classes = MyBatchJobConfiguration.class)
* public class MyBatchJobTests {
*
* @Autowired
* private JobLauncherTestUtils jobLauncherTestUtils;
*
* @Autowired
* private JobRepositoryTestUtils jobRepositoryTestUtils;
*
* @Before
* public void clearJobExecutions() {
* this.jobRepositoryTestUtils.removeJobExecutions();
* }
*
* @Test
* public void testMyJob() throws Exception {
* // given
* JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
*
* // when
* JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters);
*
* // then
* Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
* }
*
* }
* </pre>
*
* For JUnit 5, this annotation can be used without having to manually register the
* {@link SpringExtension} since {@code @SpringBatchTest} is meta-annotated with
* {@code @ExtendWith(SpringExtension.class)}:
*
* <pre class="code">
* @SpringBatchTest
* @ContextConfiguration(classes = MyBatchJobConfiguration.class)
* public class MyBatchJobTests {
*
* @Autowired
* private JobLauncherTestUtils jobLauncherTestUtils;
*
* @Autowired
* private JobRepositoryTestUtils jobRepositoryTestUtils;
*
* @BeforeEach
* public void clearJobExecutions() {
* this.jobRepositoryTestUtils.removeJobExecutions();
* }
*
* @Test
* public void testMyJob() throws Exception {
* // given
* JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
*
* // when
* JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters);
*
* // then
* Assertions.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
* }
*
* }
* </pre>
*
* <p><strong>
* It should be noted that {@link JobLauncherTestUtils} requires a
* {@link org.springframework.batch.core.Job} bean and that
* {@link JobRepositoryTestUtils} requires a {@link javax.sql.DataSource} bean.
* Since this annotation registers a {@link JobLauncherTestUtils} and a
* {@link JobRepositoryTestUtils} in the test context, it is expected that the
* test context contains a single autowire candidate for a
* {@link org.springframework.batch.core.Job} and a {@link javax.sql.DataSource}
* (either a single bean definition or one that is
* annotated with {@link org.springframework.context.annotation.Primary}).
* </strong></p>
*
* @author Mahmoud Ben Hassine
* @since 4.1
* @see JobLauncherTestUtils
* @see JobRepositoryTestUtils
* @see StepScopeTestExecutionListener
* @see JobScopeTestExecutionListener
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@TestExecutionListeners(
listeners = {StepScopeTestExecutionListener.class, JobScopeTestExecutionListener.class},
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS
)
@ExtendWith(SpringExtension.class)
public @interface SpringBatchTest {
}