Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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
Java @ContextConfiguration不是通过Springockito继承的_Java_Spring_Spring Test_Springockito - Fatal编程技术网

Java @ContextConfiguration不是通过Springockito继承的

Java @ContextConfiguration不是通过Springockito继承的,java,spring,spring-test,springockito,Java,Spring,Spring Test,Springockito,我目前正在使用springockito注释,这需要@RunWith和@ContextConfiguration注释才能工作。我想把这些注释放在我的测试的超类上,但似乎无法让它工作 超类: @RunWith(SpringJUnit4ClassRunner.class) @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class}

我目前正在使用
springockito注释
,这需要
@RunWith
@ContextConfiguration
注释才能工作。我想把这些注释放在我的测试的超类上,但似乎无法让它工作

超类:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class})
@ContextConfiguration(loader = SpringockitoContextLoader.class, locations = "classpath:spring/test-context.xml")
public class MyTestSuperclass {
    //...
testclass示例:

public class MyTest extends MyTestSuperclass {
    @Inject
    @ReplaceWithMock
    private MyService myService;
    //...
使用此代码,
myService
不会被模拟替换

但是,如果我把它改成

@ContextConfiguration
public class MyTest extends MyTestSuperclass {
    //...
…它起作用了

有没有办法避免将
@ContextConfiguration
添加到我的所有测试类中?这可能在较新版本的
Spring
/
Spring测试中已经修复了吗?据我所知,它能够从超类继承
位置
部分,而不注释子类,但是
加载程序
部分在子类中没有注释时无法工作。我使用的是
Spring test
的3.2.1.RELEASE版本

下面是一个示例项目,它显示了错误:


使用注释而不是使用超类

例如:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class})
@ContextConfiguration(loader = SpringockitoContextLoader.class, locations = "classpath:spring/test-context.xml")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestConfiguration {
}
然后,用

@TestConfiguration 
public class MyTest  {
    @Inject
    @ReplaceWithMock
    private MyService myService;
    //...

IDE的语法突出显示可能会抱怨不允许注入,这取决于您使用的是什么,但我的初始测试使用的是这样一种方法,即使用注释,而不是使用超类

例如:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class})
@ContextConfiguration(loader = SpringockitoContextLoader.class, locations = "classpath:spring/test-context.xml")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestConfiguration {
}
然后,用

@TestConfiguration 
public class MyTest  {
    @Inject
    @ReplaceWithMock
    private MyService myService;
    //...
IDE的语法突出显示可能会抱怨不允许注入,具体取决于您使用的是什么,但我的初始测试表明,这是由于一个错误

@ContextConfiguration
实际上是继承的,自Spring2.5引入以来一直如此。此外,配置的
加载器
(即本例中的
SpringockitoContextLoader
)也被继承,自Spring 3.0以来一直如此

这里的问题是
SpringockitoContextLoader
错误地处理声明类(即,用
@ContextConfiguration
注释的类),而不是实际的测试类(可以是继承
@ContextConfiguration
声明的子类)

经过彻底的调试,结果证明解决方案非常简单(实际上比原来的
SpringockitoContextLoader
实现更简单)

以下
修补的SpringockitoContextLoader
应该可以作为损坏的
SpringockitoContextLoader
的替代品

import org.kubek2k.springockito.annotations.internal.Loader;
导入org.springframework.context.ConfigurableApplicationContext;
导入org.springframework.context.support.GenericaApplicationContext;
导入org.springframework.test.context.MergedContextConfiguration;
导入org.springframework.test.context.support.GenericXmlContextLoader;
公共类PatchedSpringLockitoContextLoader扩展了GenericXmlContextLoader{
专用最终装载机=新装载机();
@凌驾
受保护的void prepareContext(ConfigurableApplicationContext上下文,MergedContextConfiguration mergedConfig){
super.prepareContext(上下文,合并配置);
this.loader.defineMocksAndSpies(mergedConfig.getTestClass());
}
@凌驾
受保护的void customizeContext(GenericaApplicationContext上下文){
超级文本(上下文);
this.loader.registerMocksAndSpies(上下文);
}
}
问候,

Sam(SpringTestContext框架的作者)

这是由于一个错误

@ContextConfiguration
实际上是继承的,自Spring2.5引入以来一直如此。此外,配置的
加载器
(即本例中的
SpringockitoContextLoader
)也被继承,自Spring 3.0以来一直如此

这里的问题是
SpringockitoContextLoader
错误地处理声明类(即,用
@ContextConfiguration
注释的类),而不是实际的测试类(可以是继承
@ContextConfiguration
声明的子类)

经过彻底的调试,结果证明解决方案非常简单(实际上比原来的
SpringockitoContextLoader
实现更简单)

以下
修补的SpringockitoContextLoader
应该可以作为损坏的
SpringockitoContextLoader
的替代品

import org.kubek2k.springockito.annotations.internal.Loader;
导入org.springframework.context.ConfigurableApplicationContext;
导入org.springframework.context.support.GenericaApplicationContext;
导入org.springframework.test.context.MergedContextConfiguration;
导入org.springframework.test.context.support.GenericXmlContextLoader;
公共类PatchedSpringLockitoContextLoader扩展了GenericXmlContextLoader{
专用最终装载机=新装载机();
@凌驾
受保护的void prepareContext(ConfigurableApplicationContext上下文,MergedContextConfiguration mergedConfig){
super.prepareContext(上下文,合并配置);
this.loader.defineMocksAndSpies(mergedConfig.getTestClass());
}
@凌驾
受保护的void customizeContext(GenericaApplicationContext上下文){
超级文本(上下文);
this.loader.registerMocksAndSpies(上下文);
}
}
问候,


Sam(Spring TestContext Framework的作者)

我试图避免注释每一个测试类,将
@ContextConfiguration
更改为
@TestConfiguration
并没有真正的帮助(除非我可以将
@TestConfiguration
放在一个超类上并避免放在子类上。)但我不知道这是可能的,很高兴知道:)我没有要确认的类框架设置,但是您可以将
@Inherited
放入
@TestConfiguration
的定义中。然后p