lundi 24 août 2015

How to set different classpath in an unit test to load resource with Spring

I would like to create 2 test case with JUnit and Spring that requires both the same classpath resource batch-configuration.properties but content of this file differ depending on test.

Actually in my maven project, I create these file tree :

  • src/test/resources/test1/batch-configuration.properties
  • src/test/resources/test2/batch-configuration.properties

But how can I define my root classpath depending on my test case (files are loaded in ExtractionBatchConfiguration using classpath:batch-configuration.properties)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ExtractionBatchConfiguration.class }, loader = AnnotationConfigContextLoader.class)
@PropertySource("classpath:test1/batch-configuration.properties") // does not override ExtractionBatchConfiguration declaration
public class ExtractBatchTestCase {
 // ...
}

Config :

@Configuration
@EnableAutoConfiguration
@PropertySources({
    @PropertySource("batch-default-configuration.properties"),
    @PropertySource("batch-configuration.properties")
})
public class ExtractionBatchConfiguration ...

I am using Spring 4.0.9 (I cannot use 4.1.x) and JUnit 4.11

Aucun commentaire:

Enregistrer un commentaire