Skip to content

Instantly share code, notes, and snippets.

@x97mdr
Last active October 9, 2018 18:59
Show Gist options
  • Save x97mdr/6f456c9eaa04169960977a73c59cde44 to your computer and use it in GitHub Desktop.
Save x97mdr/6f456c9eaa04169960977a73c59cde44 to your computer and use it in GitHub Desktop.
Spring batch test configuration
package com.saba.shinypeasant.test
import org.springframework.batch.test.JobScopeTestExecutionListener
import org.springframework.batch.test.StepScopeTestExecutionListener
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.TestExecutionListeners
import java.lang.annotation.Inherited
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
@Inherited
@SpringBootTest(properties = ["spring.batch.job.enabled=false"])
@TestExecutionListeners(
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS,
listeners = [
StepScopeTestExecutionListener::class,
JobScopeTestExecutionListener::class
]
)
annotation class BatchJobTest
package com.saba.shinypeasant.batch.processor
import com.saba.shinypeasant.config.PeasantConfigurationProperties
import com.saba.shinypeasant.test.BatchJobTest
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.batch.core.ExitStatus
import org.springframework.batch.core.Job
import org.springframework.batch.core.JobParametersBuilder
import org.springframework.batch.test.JobLauncherTestUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.test.context.junit4.SpringRunner
import java.util.*
@RunWith(SpringRunner::class)
@BatchJobTest
class ReadJiraIssueJobTest {
@Autowired
@Qualifier("readJiraIssuesJob")
lateinit var jobLauncherTestUtils: JobLauncherTestUtils
@Autowired
@Qualifier("readJiraIssuesJob")
lateinit var readJiraIssuesJob: Job
@Autowired
lateinit var peasantConfigurationProperties: PeasantConfigurationProperties
@Test
fun `Can launch job at Halogen Jira`() {
peasantConfigurationProperties.jiraServers["halogen"]?.searchJql = "key = \"HGN-1000\""
val jobParameters = JobParametersBuilder()
.addString("jira.username", peasantConfigurationProperties.jiraServers["halogen"]?.username)
.addString("jira.password", peasantConfigurationProperties.jiraServers["halogen"]?.password)
.addString("jira.base.url", peasantConfigurationProperties.jiraServers["halogen"]?.baseUrl)
.addString("jira.project.key", "HGN")
.addDate("timestamp", Date())
val jobExecution = jobLauncherTestUtils.launchJob(jobParameters.toJobParameters())
assertThat(jobExecution.exitStatus).isEqualTo(ExitStatus.COMPLETED)
}
@TestConfiguration
class BatchTestConfig {
@Bean
@Qualifier("readJiraIssuesJob")
fun jobLauncherTestUtils(readJiraIssuesJob: Job): JobLauncherTestUtils {
val jobLauncherTestUtils = JobLauncherTestUtils()
jobLauncherTestUtils.job = readJiraIssuesJob
return jobLauncherTestUtils
}
}
}
@x97mdr
Copy link
Author

x97mdr commented Oct 9, 2018

I updated the gist with the code for the annotation class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment