0

I'm using Spring Boot 1.3.3.RELEASE with Spring Batch 3.0.6.RELEASE and am having trouble getting Spring Batch/Spring Boot to not auto-start my jobs.

This question is similar to how to stop spring batch scheduled jobs from running at first time when executing the code?

I have the following test class:

@RunWith(SpringJUnit4ClassRunner.class)
@Configuration
@ComponentScan(useDefaultFilters = false,
    includeFilters = @ComponentScan.Filter(value = GetMarkerViaSpringBatchApplicationTest.class,
        type = FilterType.ASSIGNABLE_TYPE) )
@EnableBatchProcessing
//@IntegrationTest({"spring.batch.job.enabled=false"})
@EnableAutoConfiguration
@TestPropertySource(
    locations = {"classpath:env.properties", "classpath:application.properties", "classpath:database.properties"},
    properties = {"spring.batch.job.enabled=false"})
@SpringApplicationConfiguration(classes = {GetMarkerViaSpringBatchApplicationTest.class},
    locations = {"classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml"})
public class GetMarkerViaSpringBatchApplicationTest {
  @Autowired
  private JobLauncherTestUtils jobLauncherTestUtils;

  @Test
  public void testLaunchJob() throws Exception {
    JobExecution jobExecution = jobLauncherTestUtils.launchJob();
     assertThat(jobExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED);
  }
}

In the same directory (package) as the test class I have config file GetMarkerViaSpringBatchApplicationTest-context.xml.

GetMarkerViaSpringBatchApplicationTest-context.xml contents:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="classpath:/META-INF/spring/get-marker-job-new.xml" />

    <bean id="jobLauncherTestUtils" class="org.springframework.batch.test.JobLauncherTestUtils" />

    <bean id="batchConfigurer" class="org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer">
        <constructor-arg name="dataSource" ref="hsqlDataSource" />
    </bean>
</beans>

get-marker-job-new.xml contents:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:batch="http://www.springframework.org/schema/batch" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:file="http://www.springframework.org/schema/integration/file" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd    
    http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <import resource="classpath:META-INF/spring/application-context.xml" />
    <import resource="classpath:META-INF/spring/database.xml" />

    <!-- The @EnableBatchProcessing on the main class sets up: job-repository, jobLauncher, and jobRepository -->
    <!-- Spring Boot with Spring Batch doesn't like multiple DataSources since it doesn't know which one -->
    <!-- to pick for Spring Batch. This class will therefore coerce to pick the proper HSQL dataSource. -->
    <!-- See https://stackoverflow.com/questions/25540502/use-of-multiple-datasources-in-spring-batch -->
    <bean id="batchConfigurer" class="org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer">
        <constructor-arg name="dataSource" ref="hsqlDataSource" />
    </bean>

    <bean id="foo" class="myClass" />

    <bean id="myTasklet" class="org.springframework.batch.core.step.tasklet.MethodInvokingTaskletAdapter">
        <property name="targetObject" ref="myClass" />
        <property name="targetMethod" value="deliver" />
    </bean>

    <batch:job id="batchJob" restartable="false">
        <batch:step id="step1">
            <batch:tasklet ref="myTasklet"/>
        </batch:step>
    </batch:job>
</beans>    

application-context.xml contents:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
    xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd
        http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <import resource="classpath:META-INF/spring/database.xml" />

    <!-- load properties from config files -->
    <context:property-placeholder location="classpath:env.properties,classpath:application.properties,classpath:database.properties" />

    <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
</beans>

database.xml contents:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

    <bean id="myDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.myco.driverClassName}" />
        <property name="username" value="${jdbc.myco.username}" />
        <property name="password" value="${jdbc.myco.encpassword}" />
        <property name="jdbcUrl" value="${jdbc.myco.url}" />
        <property name="maximumPoolSize" value="${jdbc.myco.pool.maxactive}" />
    </bean>
    <alias alias="dataSource" name="myDataSource" />

    <bean id="hsqlDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close" primary="true">
        <property name="driverClassName" value="${jdbc.hsqldb.driverClassName}" />
        <property name="username" value="${jdbc.hsqldb.username}" />
        <property name="password" value="${jdbc.hsqldb.password}" />
        <property name="jdbcUrl" value="${jdbc.hsqldb.url}" />
        <property name="maximumPoolSize" value="${jdbc.hsqldb.pool.maxactive}" />
    </bean>

</beans>

application.properties (in root dir which is also in the classpath):

spring.datasource.platform=sqlserver
spring.profiles.active=local
spring.batch.job.enabled=true
spring.batch.initializer.enabled=true
spring.batch.schema=classpath:org/springframework/batch/core/schema-hsqldb.sql

Note that spring.batch.job.enabled=true in the application.properties but false in the @IntegrationTest and the @TestPropertySource.
Whenever I set spring.batch.job.enabled=true in the application.properties regardless of trying various combos of spring.batch.job.enabled=false in the @IntegrationTest and the @TestPropertySource whenever I run this test the job is getting kicked off before the jobLauncherTestUtils.launchJob() gets called.
Logs from the run:

15:52:49,083 (o.s.test.context.support.AbstractDirtiesContextTestExecutionListener) - Before test class: context [DefaultTestContext@1733f619 testClass = GetMarkerViaSpringBatchApplicationTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@5461ef35 testClass = GetMarkerViaSpringBatchApplicationTest, locations = '{classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml}', classes = '{class xxx.myco.batch.client.GetMarkerViaSpringBatchApplicationTest}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:env.properties, classpath:application.properties, classpath:database.properties}', propertySourceProperties = '{spring.batch.job.enabled=false, marker=SELC_ALPHA_DONE, marker_date=2016/06/21, interval=1, times=1, is_fail=true, fail_path=${pkg.out}\\failed_alpha_report}', contextLoader = 'o.s.boot.test.SpringApplicationContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null].

15:52:49,099 (o.s.test.context.support.DependencyInjectionTestExecutionListener) - Performing dependency injection for test context [[DefaultTestContext@1733f619 testClass = GetMarkerViaSpringBatchApplicationTest, testInstance = xxx.myco.batch.client.GetMarkerViaSpringBatchApplicationTest@6769360f, testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@5461ef35 testClass = GetMarkerViaSpringBatchApplicationTest, locations = '{classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml}', classes = '{class xxx.myco.batch.client.GetMarkerViaSpringBatchApplicationTest}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:env.properties, classpath:application.properties, classpath:database.properties}', propertySourceProperties = '{spring.batch.job.enabled=false, marker=SELC_ALPHA_DONE, marker_date=2016/06/21, interval=1, times=1, is_fail=true, fail_path=${pkg.out}\\failed_alpha_report}', contextLoader = 'o.s.boot.test.SpringApplicationContextLoader', parent = [null]]]].

15:52:50,572 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]]
15:52:50,572 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]]
15:52:50,572 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false'

15:52:50,665 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]]
15:52:50,665 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]]
15:52:50,665 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false'

15:52:50,665 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]]
15:52:50,665 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]]
15:52:50,665 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false'

15:52:52,132 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]]
15:52:52,132 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]]
15:52:52,132 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false'

15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]]
15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]]
15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false'

15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]]
15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]]
15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false'

15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]]
15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]]
15:53:00,861 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false'

15:53:00,876 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [database.properties]]
15:53:00,876 (o.s.core.env.PropertySourcesPropertyResolver) - Searching for key 'spring.batch.job.enabled' in [class path resource [application.properties]]
15:53:00,876 (o.s.core.env.PropertySourcesPropertyResolver) - Found key 'spring.batch.job.enabled' in [class path resource [application.properties]] with type [String] and value 'false'
...
   BatchAutoConfiguration#jobExplorer did not match
      - @ConditionalOnMissingBean (types: o.s.batch.core.explore.JobExplorer; SearchStrategy: all) found the following [jobExplorer] (OnBeanCondition)

   BatchAutoConfiguration#jobLauncherCommandLineRunner did not match
      - @ConditionalOnMissingBean (types: o.s.boot.autoconfigure.batch.JobLauncherCommandLineRunner; SearchStrategy: all) found no beans (OnBeanCondition)
      - @ConditionalOnProperty expected 'true' for properties spring.batch.job.enabled (OnPropertyCondition)

   BatchAutoConfiguration.JpaBatchConfiguration did not match
      - required @ConditionalOnClass classes not found: javax.persistence.EntityManagerFactory (OnClassCondition)
...
15:53:02,225 (o.s.test.context.cache.DefaultCacheAwareContextLoaderDelegate) - Storing ApplicationContext in cache under key [[MergedContextConfiguration@5461ef35 testClass = GetMarkerViaSpringBatchApplicationTest, locations = '{classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml}', classes = '{class xxx.myco.batch.client.GetMarkerViaSpringBatchApplicationTest}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:env.properties, classpath:application.properties, classpath:database.properties}', propertySourceProperties = '{spring.batch.job.enabled=false, marker=SELC_ALPHA_DONE, marker_date=2016/06/21, interval=1, times=1, is_fail=true, fail_path=${pkg.out}\\failed_alpha_report}', contextLoader = 'o.s.boot.test.SpringApplicationContextLoader', parent = [null]]]

15:53:02,256 (o.s.test.context.support.AbstractDirtiesContextTestExecutionListener) - Before test method: context [DefaultTestContext@1733f619 testClass = GetMarkerViaSpringBatchApplicationTest, testInstance = xxx.myco.batch.client.GetMarkerViaSpringBatchApplicationTest@6769360f, testMethod = testLaunchJob@GetMarkerViaSpringBatchApplicationTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@5461ef35 testClass = GetMarkerViaSpringBatchApplicationTest, locations = '{classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml}', classes = '{class xxx.myco.batch.client.GetMarkerViaSpringBatchApplicationTest}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:env.properties, classpath:application.properties, classpath:database.properties}', propertySourceProperties = '{spring.batch.job.enabled=false, marker=SELC_ALPHA_DONE, marker_date=2016/06/21, interval=1, times=1, is_fail=true, fail_path=${pkg.out}\\failed_alpha_report}', contextLoader = 'o.s.boot.test.SpringApplicationContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null], method annotated with @DirtiesContext [false] with mode [null].

15:53:04,669 (o.s.test.context.support.AbstractDirtiesContextTestExecutionListener) - After test method: context [DefaultTestContext@1733f619 testClass = GetMarkerViaSpringBatchApplicationTest, testInstance = xxx.myco.batch.client.GetMarkerViaSpringBatchApplicationTest@6769360f, testMethod = testLaunchJob@GetMarkerViaSpringBatchApplicationTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@5461ef35 testClass = GetMarkerViaSpringBatchApplicationTest, locations = '{classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml}', classes = '{class xxx.myco.batch.client.GetMarkerViaSpringBatchApplicationTest}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:env.properties, classpath:application.properties, classpath:database.properties}', propertySourceProperties = '{spring.batch.job.enabled=false, marker=SELC_ALPHA_DONE, marker_date=2016/06/21, interval=1, times=1, is_fail=true, fail_path=${pkg.out}\\failed_alpha_report}', contextLoader = 'o.s.boot.test.SpringApplicationContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null], method annotated with @DirtiesContext [false] with mode [null].

15:53:04,685 (o.s.test.context.support.AbstractDirtiesContextTestExecutionListener) - After test class: context [DefaultTestContext@1733f619 testClass = GetMarkerViaSpringBatchApplicationTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@5461ef35 testClass = GetMarkerViaSpringBatchApplicationTest, locations = '{classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml}', classes = '{class xxx.myco.batch.client.GetMarkerViaSpringBatchApplicationTest}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:env.properties, classpath:application.properties, classpath:database.properties}', propertySourceProperties = '{spring.batch.job.enabled=false, marker=SELC_ALPHA_DONE, marker_date=2016/06/21, interval=1, times=1, is_fail=true, fail_path=${pkg.out}\\failed_alpha_report}', contextLoader = 'o.s.boot.test.SpringApplicationContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null].

I've read http://forum.spring.io/forum/spring-projects/batch/748038-i-do-not-want-enablebatchprocessing-to-launch-a-job and how to stop spring batch scheduled jobs from running at first time when executing the code? but can't seem to get the jobLauncher to NOT auto-start.
My understanding is that application.properties properties should be overriden by the 2 places I'm trying to set this directly within the Test but BatchAutoConfiguration doesn't seem to honor that override.
If I set spring.batch.job.enabled=false (or comment that property out) in the application.properties and try to drive it from either @IntegrationTest or @TestPropertySource it behaves as expected.
Is my understanding of the overriding of values incorrect?

Any help would be appreciated.

Community
  • 1
  • 1
Tony Falabella
  • 335
  • 1
  • 6
  • 17
  • `@ComponentScan` and `@EnableAutoConfiguration` do not belong on a test class. It's just noise because they don't do anything. – Dave Syer Jul 03 '16 at 06:53
  • @DaveSyer This works (notice `@EnableAutoConfiguration`): @RunWith(SpringJUnit4ClassRunner.class) @EnableBatchProcessing @IntegrationTest({"spring.batch.job.enabled=false"}) @EnableAutoConfiguration @TestPropertySource(locations = {"classpath:env.properties", "classpath:database.properties"}) @SpringApplicationConfiguration(classes = {GetMarkerViaSpringBatchApplicationTest.class}, locations = {"classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml"}) public class GetMarkerViaSpringBatchApplicationTest { – Tony Falabella Jul 05 '16 at 17:02
  • @DaveSyer This doesn't work (notice no `@EnableAutoConfiguration`) Get "No bean named 'jobRepository' is defined" error: @RunWith(SpringJUnit4ClassRunner.class) @EnableBatchProcessing @IntegrationTest({"spring.batch.job.enabled=false"}) @TestPropertySource(locations = {"classpath:env.properties", "classpath:database.properties"}) @SpringApplicationConfiguration(classes = {GetMarkerViaSpringBatchApplicationTest.class}, locations = {"classpath:**/GetMarkerViaSpringBatchApplicationTest-context.xml"}) public class GetMarkerViaSpringBatchApplicationTest { – Tony Falabella Jul 05 '16 at 17:04
  • @DaveSyer This doesn't work either (notice removed `@EnableAutoConfiguration`) Get "No bean named 'jobRepository' is defined" error: @RunWith(SpringJUnit4ClassRunner.class) @EnableBatchProcessing @IntegrationTest({"spring.batch.job.enabled=false"}) @TestPropertySource(locations = {"classpath:env.properties", "classpath:database.properties"}) @SpringApplicationConfiguration public class GetMarkerViaSpringBatchApplicationTest { – Tony Falabella Jul 05 '16 at 17:09
  • Yeah, well you are using a test class as configuration. I get that. It's just not very common, and I expect it might cause other issues, so I pointed it out in a comment. – Dave Syer Jul 05 '16 at 17:53
  • 1
    Hi @tonyFalabella Did you get to stop auto execution at startup ? – mayank vats Oct 25 '19 at 07:17

1 Answers1

1

If you add application.properties to @TestPropertySource then it ends up overriding the other stuff. You don't need it there if you are using spring boot.

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
  • Yes indeed this appears to be the issue. If you could take a look at the reply comments to your `@EnableAutoConfiguration` suggestion it would be helpful. I suspect my issue has to do with mixing XML files with Java classes for config files. – Tony Falabella Jul 05 '16 at 17:06