Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多级测试类层次结构中的Spring@ContextHierarchy(使用Scala和ScalaTest)_Spring_Scala_Unit Testing_Scalatest_Applicationcontext - Fatal编程技术网

多级测试类层次结构中的Spring@ContextHierarchy(使用Scala和ScalaTest)

多级测试类层次结构中的Spring@ContextHierarchy(使用Scala和ScalaTest),spring,scala,unit-testing,scalatest,applicationcontext,Spring,Scala,Unit Testing,Scalatest,Applicationcontext,与其拥有几个完整的应用程序上下文,复制和粘贴99%的内容,并在上下文应该不同的地方只添加/删除几行内容,我宁愿将所有通用内容放在一个父上下文中,即rootContext.xml。其他所有内容都是子上下文的一部分,要么合并到父上下文中,要么替换它 [1] 描述设置@ContextHierarchy和使用单元测试类层次结构所需的所有内容。但它不起作用 我的类层次结构优先: @RunWith(classOf[JUnitRunner]) @ContextHierarchy(value = Array(n

与其拥有几个完整的应用程序上下文,复制和粘贴99%的内容,并在上下文应该不同的地方只添加/删除几行内容,我宁愿将所有通用内容放在一个父上下文中,即rootContext.xml。其他所有内容都是子上下文的一部分,要么合并到父上下文中,要么替换它

[1] 描述设置@ContextHierarchy和使用单元测试类层次结构所需的所有内容。但它不起作用

我的类层次结构优先:

@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应用程序上下文文件:

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"/>
因此persistenceTestContext.xml应该在parentContext中添加一行。以前的通用内容现在在rootTestContect.xml中。然而,当运行NeedsExtraContextForPersistenceTest时,spring的组件扫描器并没有拾取Springbean进行自动连接。在父上下文中定义的扫描器似乎没有效果。为了说明这一点,当通过@ContextConfiguration和包含scanner配置的完整persistenceTestContext.xml使用标准方式时,运行测试没有问题

由于只有少数示例使用@ContextHierarchy的测试层次结构,而没有使用Scala+ScalaTest的,如果您能提供一些见解,我将非常高兴


[1]

这些注释由Spring JUnit runner@RunWithSpringJUnit4ClassRunner.class处理;如果您在scala中编写标准junit测试并使用该运行程序,那么它应该可以工作。如果您想将scalatest用于spring,您可能需要编写自己的运行程序,调用spring运行程序将要做的事情和scalatest运行程序将要做的事情。

谢谢lmm,但是scalatest附带的JUnitRunner确实在spring上运行。请看一看。我只需要将新的TestContextManagerthis.getClass.PrepareTestInstance添加到基类中,就可以使事情正常进行。所以原则上他们是同意的。正如我所说,当我将@ConfigurationContext引入混合时,问题就开始了。因此,也许还有一种方法可以完成这一任务,而不必再重复跑步者