0

I am currently playing around with springockito-annotations and this requires @RunWith and @ContextConfiguration annotations in order to work. I would like to put these annotations on a superclass of my tests, but can't seem to get it to work.

Superclass:

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

Example testclass:

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

With this code, myService is not replaced with a mock.

But, if I change it to...

@ContextConfiguration
public class MyTest extends MyTestSuperclass {
    //...

...it works.

Is there a way to avoid having to add @ContextConfiguration to all my test classes? Has this perhaps been fixed in a newer version of Spring/Spring-tests? From what I can tell it is able to inherit the locations part from the superclass without annotating the subclass, but the loader part does not work without an annotation in the subclass. I'm using version 3.2.1.RELEASE of Spring-test.

Here is a sample projec that shows the bug:

http://www.filedropper.com/springockitobug

4

2 回答 2

2

这是由于Springockito 中的一个错误

@ContextConfiguration实际上是继承的,自 Spring 2.5 引入以来一直如此。此外,配置的loader(即 SpringockitoContextLoader在这种情况下)也是继承的,自 Spring 3.0 以来一直如此。

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

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

以下PatchedSpringockitoContextLoader应该可以很好地替代损坏的SpringockitoContextLoader.

import org.kubek2k.springockito.annotations.internal.Loader;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.support.GenericXmlContextLoader;

public class PatchedSpringockitoContextLoader extends GenericXmlContextLoader {

    private final Loader loader = new Loader();

    @Override
    protected void prepareContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
        super.prepareContext(context, mergedConfig);
        this.loader.defineMocksAndSpies(mergedConfig.getTestClass());
    }

    @Override
    protected void customizeContext(GenericApplicationContext context) {
        super.customizeContext(context);
        this.loader.registerMocksAndSpies(context);
    }

}

问候,

Sam(Spring TestContext 框架的作者

于 2016-01-29T15:31:46.207 回答
0

不要使用超类,而是使用注释。

例如:

@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 的语法突出显示可能会抱怨不允许注入,这取决于您使用的,但我最初的测试有这个工作

于 2016-01-26T14:29:47.403 回答