在PowerMockRunner上运行时,为什么不能使用@InjectMocks字段匹配? [英] Why can't I use @InjectMocks field matching when running with PowerMockRunner?

查看:938
本文介绍了在PowerMockRunner上运行时,为什么不能使用@InjectMocks字段匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,其中字段注入与Mockito的 @Mock 批注与 @InjectMocks 匹配在有2个相同类型的 @Mock 的情况下不起作用。我也使用了 @Mock(name = name_of_var)语法,但是仍然失败...

I've run into an issue in which the field injection matching for Mockito's @Mock annotation for @InjectMocks is not working in the case where there are 2 @Mocks of the same type. I've used the @Mock (name = "name_of_var") syntax as well, but it still failed...

这里是要测试的类:

    import java.util.Date;
    public class Parent{

    private Date dateA;
    private Date dateB;

    public void setDateA(Date _dateA){
       dateA = _dateA;
    }

    public void setDateB(Date _dateB){
       dateB = _dateB;
    }

    public Date getDateA(){
       return dateA;
    }

    public Date getDateB(){
       return dateB;
    }

这是测试本身:

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({System.class, Parent.class})
    public class testParent{

    @Mock (name = "dateB")  private Date someOtherDate;
    @Mock (name = "dateA")  private Date someDate;    

    @InjectMocks Parent p;

    @Before
    public void setup(){
        Mockito.when(someOtherDate.getTime()).thenReturn(500l);
        PowerMockito.mockStatic(System.class);
        PowerMockito.when(System.currentTimeMillis()).thenReturn(2000l);
    }

    @Test
    public void testGetDateAGetTimeShouldReturn1000() {
        Mockito.when(someDate.getTime()).thenReturn(1000l);
        Date result = p.getDateA();
        assertEquals(1000l, result.getTime());
    }

    @Test
    public void testGetDateBGetTimeShouldReturn500() {
        Date result = p.getDateB();
        assertEquals(500l, result.getTime());   
    }

测试时,两个 assertEquals 由于 @InjectMocks 不起作用而导致 NullPointerException

When tested, both assertEquals cause a NullPointerException due to the fact that the @InjectMocks did not work.

现在,当我将 @RunWith(PowerMockRunner.class)替换为 @RunWith(MockitoJUnitRunner.class),就可以了。

Now, when I replaced @RunWith(PowerMockRunner.class) with @RunWith(MockitoJUnitRunner.class), it WORKS just fine.

此外,如果我刚刚定义了1个 Date 变量(例如, Parent.java 中的 dateA )以及要插入 ParentTest ,它将使用 PowerMockRunner.class 注入。

Also, if I had just defined 1 Date variable ( say, dateA ) in Parent.java and a matching mock to inject in ParentTest, it would inject just fine using PowerMockRunner.class.

我必须使用PowerMockRunner运行的原因.class是因为我必须能够模拟静态函数以及构造函数。

The reason I must run with PowerMockRunner.class is because I must be able to mock static functions as well as constructors.

我正在运行Junit4.12,Mockito-all-1.10.19和PowerMock-mockito-1.6.2-full。

I am running with Junit4.12, Mockito-all-1.10.19, and PowerMock-mockito-1.6.2-full.

有人看到为什么使用 PowerMockRunner.class 无法正确注入的原因吗?使用 PowerMockRunner.class 运行时是否有解决方法?

Does anyone see the cause of why it does not inject properly with PowerMockRunner.class? Is there a workaround for this while running with PowerMockRunner.class?

推荐答案

如果您要使用 PowerMockRunner ,则必须致电 MockitoAnnotations.initMocks() 以便初始化使用创建的模拟

If you are going to use PowerMockRunner, you have to call MockitoAnnotations.initMocks() in order to initialize the mocks that are created using the annotations.

但是,有几个充分的理由完全不使用 InjectMocks 。一般来说,您对测试设置的控制较少。最好只调用 new 并手动传递参数。另外,如果出现任何问题,Mockito只会默默地失败。

However, there are several good reasons not to use InjectMocks at all. Generally speaking, you have less control over how your test is set up. It's better to just call new and pass the arguments in manually. Also, if there's any sort of a problem Mockito will just fail silently. See more in this article:


Mockito将尝试仅通过构造函数注入,setter注入或属性注入来注入模拟顺序并如下所述。如果以下任何策略失败,则Mockito将不会报告失败;即您必须自己提供依赖项。

Mockito will try to inject mocks only either by constructor injection, setter injection, or property injection in order and as described below. If any of the following strategy fail, then Mockito won’t report failure; i.e. you will have to provide dependencies yourself.

这篇关于在PowerMockRunner上运行时,为什么不能使用@InjectMocks字段匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆