本文介绍了使用@RunWith Annotation 和 powerMock 时的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初我在 junits 中只使用 Mockito,所以我在 @RunWith 注释中使用 SpringJUnit4ClassRunner.class 即

Initially I was using only Mockito in junits so I was using SpringJUnit4ClassRunner.class in @RunWith annotation ie

@RunWith(SpringJUnit4ClassRunner.class)

由于 spring 依赖注入工作正常并且正在通过 bean

due to which spring dependency injection was working fine and was getting a bean through

@Autowired

Someservice someservice ;

但是现在,我也把 PowerMock 集成到里面了.

But now, I have also integrated PowerMock in it.

因此,根据 doc ,我已将 @RunWith 注释中提到的类替换为

So as per doc , I have replaced class mentioned in @RunWith annotation with

@RunWith(PowerMockRunner.class)

但是现在,someservice 即将为空.有没有办法在@RunWith 注释中同时使用 SpringJUnit4ClassRunner.classPowerMockRunner.class

but now, someservice is coming out to be null. Is there a way to use both SpringJUnit4ClassRunner.class and PowerMockRunner.class in @RunWith annotation

推荐答案

我知道这个帖子很旧,但是从 2014 年开始添加这个帖子很好,这个 拉取请求,您可以使用 @PowerMockRunnerDelegate 注释将运行上下文委托"给 SpringJUnit4ClassRunner(或任何其他跑步者真的).

I know this thread is old, but it's good to add that since 2014 and this pull request, you can use the @PowerMockRunnerDelegate annotation to "delegate" the run context to SpringJUnit4ClassRunner (or any other runner really).

上面的代码看起来像:

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@PrepareForTest(X.class);
public class MyTest {

    // Tests goes here
    ...
}

有了这个注解,你就不再需要 PowerMock 规则了!

With this annotation, you don't need the PowerMock rule anymore !

这篇关于使用@RunWith Annotation 和 powerMock 时的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 12:57