-1

I am writing integration tests for a Spring Batch Project, which has the following configuration

@Configuration
@EnableScheduling
@PropertySource("application.properties")
public class BatchConfig(
     private JobBuilderFactory factory;
     public BatchConfig(JobBuilderFactory factory) {
           this.factory = factory;
     }
     
     @Bean
     public Step someStep {
       // step implementation
     }
}

I created the following base test class

@ContextConfiguration(classes=BatchConfig.class)
@SpringBootTest
public class BatchTestBase {
       @Autowired 
       Step someStep
}

When I extend this class and try running it, I get the following error

Parameter 0 of constructor in BatchConfig required a bean of type JobBuilderFactory that could not be found

Is there any way to extend BatchConfig to XML or add constructor parameters here to access the beans?

kozmo
  • 4,024
  • 3
  • 30
  • 48
  • What you _probably_ should do is have an `@TestConfiguration` class that provides a `JobBuilderFactory` bean. Note that `@SpringBootTest` (which says "run the application as it will really be assembled, except that I might mock some of the beans) doesn't typically combine with `@ContextConfiguration`, which is for providing a test-specific configuration. – chrylis -cautiouslyoptimistic- Sep 14 '20 at 18:33

2 Answers2

0

Create test context configuration and use together with a configuration witch need to test in @ContextConfiguration

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {
        BatchTestBase.ContextConfiguration.class,
        BatchConfig.class
})
public class BatchTestBase {

    @Configuration
    static class ContextConfiguration {
        @Bean
        JobBuilderFactory factory() {
            //return fake, mock or stub
        }

    }

    @Autowired
    Step someStep;

    @Test
    public void shouldExecuteStep() {
       //some test
    }
}
kozmo
  • 4,024
  • 3
  • 30
  • 48
0

Try to inject through set instead of constructor, as it follows :

 public setFactory(JobBuilderFactory factory) {
           this.factory = factory;
 }
Guilherme Alencar
  • 1,243
  • 12
  • 21