mercredi 28 janvier 2015

Spring @ContextHierarchy in a multilevel test class hierarchy (using Scala and ScalaTest)

Instead of having several full blown applications contexts, copy&pasting 99% of the content and adding/removing only a few lines where the context should differ, I'd rather have all the generic stuff in one parent context i.e. "rootContext.xml". Everything else would be part of the child context and either be merged into the parent context or replace it.


[1] describes all you need to set up a @ContextHierarchy and use a unit test class hierarchy. But it isn't working.


My class hierarchy first:



@RunWith(classOf[JUnitRunner])
@ContextHierarchy(value = Array(new ContextConfiguration(locations = Array("classpath:/rootTestContext.xml"), name = "root")))
class TestBase extends FunSuite {...}

@ContextHierarchy(value = Array(new ContextConfiguration(locations = Array("classpath:/persistenceTestContext.xml"), name = "root", ???inheritLocations = false???)))
class PersistenceTestBase extends TestBase {...}

class BasicTest extends TestBase {
test("blah") { ... }
}

class NeedsExtraContextForPersistenceTest extends PersistenceTestBase {
test("persist!") { ... }
}


Spring application context files:



rootTestContext.xml:

<import resource="classpath:/configfiles/spring/genericCaching.xml"/>
<context:annotation-config/>
<context:component-scan base-package="my.package.cm" />
<context:component-scan base-package="my.package.package2.cm" />

persistenceTestContext.xml:

<import resource="classpath:/configfiles/spring/persistence.xml"/>


So persistenceTestContext.xml should add one line to the parentContext. The generic stuff that used to be there is now in rootTestContect.xml. However when running NeedsExtraContextForPersistenceTest spring's component scanner isn't picking up spring beans for autowiring. It seems as if the scanner as defined in the parent context has no effect. Just to make this clear, there is no issue running the test when using the standard way (via @ContextConfiguration) and a complete persistenceTestContext.xml (that includes the scanner config)


Since there are only a few examples using test hierarchies with @ContextHierarchy and none using Scala + ScalaTest I'd be glad if you could provide some insight.


[1] http://ift.tt/1zyFew0


Aucun commentaire:

Enregistrer un commentaire