001/*
002 * Copyright 2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.test.context;
017
018import java.lang.annotation.Documented;
019import java.lang.annotation.ElementType;
020import java.lang.annotation.Inherited;
021import java.lang.annotation.Retention;
022import java.lang.annotation.RetentionPolicy;
023import java.lang.annotation.Target;
024
025import org.springframework.batch.test.JobLauncherTestUtils;
026import org.springframework.batch.test.JobRepositoryTestUtils;
027import org.springframework.batch.test.JobScopeTestExecutionListener;
028import org.springframework.batch.test.StepScopeTestExecutionListener;
029import org.springframework.test.context.TestExecutionListeners;
030
031/**
032 * Annotation that can be specified on a test class that runs Spring Batch based tests.
033 * Provides the following features over the regular <em>Spring TestContext Framework</em>:
034 * <ul>
035 * <li>Registers a {@link JobLauncherTestUtils} bean with the
036 * {@link BatchTestContextCustomizer#JOB_LAUNCHER_TEST_UTILS_BEAN_NAME} which can be used
037 * in tests for launching jobs and steps.
038 * </li>
039 * <li>Registers a {@link JobRepositoryTestUtils} bean
040 * with the {@link BatchTestContextCustomizer#JOB_REPOSITORY_TEST_UTILS_BEAN_NAME}
041 * which can be used in tests setup to create or remove job executions.
042 * </li>
043 * <li>Registers the {@link StepScopeTestExecutionListener} and {@link JobScopeTestExecutionListener}
044 * as test execution listeners which are required to test step/job scoped beans.
045 * </li>
046 * </ul>
047 * <p>
048 * A typical usage of this annotation is like:
049 *
050 * <pre class="code">
051 * &#064;RunWith(SpringRunner.class)
052 * &#064;SpringBatchTest
053 * &#064;ContextConfiguration(classes = MyBatchJobConfiguration.class)
054 * public class MyBatchJobTests {
055 *
056 *    &#064;@Autowired
057 *    private JobLauncherTestUtils jobLauncherTestUtils;
058 *
059 *    &#064;@Autowired
060 *    private JobRepositoryTestUtils jobRepositoryTestUtils;
061 *
062 *    &#064;Before
063 *    public void clearJobExecutions() {
064 *       this.jobRepositoryTestUtils.removeJobExecutions();
065 *    }
066 *
067 *    &#064;Test
068 *    public void testMyJob() throws Exception {
069 *       // given
070 *       JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
071 *
072 *       // when
073 *       JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters);
074 *
075 *       // then
076 *       Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
077 *    }
078 *
079 * }
080 * </pre>
081 *
082 * @author Mahmoud Ben Hassine
083 * @since 4.1
084 * @see JobLauncherTestUtils
085 * @see JobRepositoryTestUtils
086 * @see StepScopeTestExecutionListener
087 * @see JobScopeTestExecutionListener
088 */
089@Target(ElementType.TYPE)
090@Retention(RetentionPolicy.RUNTIME)
091@Documented
092@Inherited
093@TestExecutionListeners(
094                listeners = {StepScopeTestExecutionListener.class, JobScopeTestExecutionListener.class},
095                mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS
096)
097public @interface SpringBatchTest {
098}