简体   繁体   中英

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 ie "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://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#testcontext-ctx-management-ctx-hierarchies

Those annotations are processed by the Spring JUnit runner, @RunWith(SpringJUnit4ClassRunner.class) ; if you write standard junit tests (in scala) and use that runner then it should work. If you want to use scalatest with this spring stuff you'll perhaps need to perhaps write your own runner that invokes both the things the spring runner would do and the things that the scalatest runner would do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM