有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Springockito没有继承java@ContextConfiguration

我目前正在玩springockito-annotations,这需要@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-tests中已经修复了吗?据我所知,它能够从超类继承locations部分,而不注释子类,但是loader部分在子类中没有注释时无法工作。我使用的是3.2.1版。发布Spring-test

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

http://www.filedropper.com/springockitobug


共 (2) 个答案

  1. # 1 楼答案

    这是由于bug in 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框架的作者)

  2. # 2 楼答案

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

    例如:

    @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的语法突出显示可能会抱怨不允许注入,这取决于您使用的是哪种方法,但我的初始测试表明这是可行的