I am having trouble loading application context from junit tests for my spring batch tests. I referred spring documentation https://docs.spring.io/spring-batch/trunk/reference/html/testing.html as well as every available information I could find on web, but still cannot get a simple junit test work.
I am using annotations to load the app context, code below. My aim is to be able to test individual steps.
I also cloned some examples from web, but when run in my local machine, they give me same error- unable to load application context... this got me thinking on how these tests are suppose to run? Run As -> junit tests is how all the unit tests run... isn't it?
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {BatchProcessingConfiguration.class, RestTemplateConfig.class, SftpConfig.class, DbConfig.class, RuntimeSessionFactoryLocator.class},
loader = AnnotationConfigContextLoader.class)
public class BatchProcessingJobApplicationTests extends AbstractTransactionalJUnit4SpringContextTests {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dataSource.setUrl("url");
dataSource.setUsername("username");
dataSource.setPassword("password");
return dataSource;
}
@Test
public void testJob() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
Assert.assertEquals("COMPLETED", jobExecution.getExitStatus().getExitCode());
}
}
Code is from spring documentation.
I am new to this stuff. Thanks for your help.
Stacktrace:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
... ...
WORKING CODE:
@RunWith(SpringRunner.class)
@SpringBatchTest
@ContextConfiguration(classes = {BatchProcessingJobApplication.class, DataSourceConfiguration.class, JobconfigurationTest.class, BatchProperties.class}, initializers=ConfigFileApplicationContextInitializer.class)
@ActiveProfiles("test")
public class BatchProcessingJobApplicationTests {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
public void testStep() throws Exception {
// given
JobParameters jobParameters = jobLauncherTestUtils.getUniqueJobParameters();
// when
JobExecution jobExecution = jobLauncherTestUtils.launchStep("jobName", jobParameters);
// then
Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
}
}
public class DataSourceConfiguration {
@Bean
public DataSource dataSource(){
SQLServerConnectionPoolDataSource dataSource = new SQLServerConnectionPoolDataSource();
dataSource.setURL(url);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;
}
}
@Configuration
public class JobconfigurationTest {
@Bean
public JobLauncherTestUtils jobLauncherTestUtils() {
return new JobLauncherTestUtils();
}
}
Hope this helps someone like me.