Mockito,@InjectMocks 最终字段的奇怪行为

Mockito, @InjectMocks strange behaviour with final fields(Mockito,@InjectMocks 最终字段的奇怪行为)
本文介绍了Mockito,@InjectMocks 最终字段的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我看到我认为是错误的行为.@InjectMocks 似乎并没有在每个测试方法之前创建一个新的测试主题.@Mock 在哪里.在以下示例中,如果 Subject.section 是最后一个 @Test 失败.如果它不是最终的,则两者都通过.我目前的解决方法是使用@BeforeClass,但这并不理想.

I am seeing behaviour that I believe is a bug. @InjectMocks does not seem to create a new test subject before every test method. Where as @Mock does. In the following example, if Subject.section is final one @Test fails. If its not final both pass. My current workaround is to use @BeforeClass, but this is not ideal.

主题.java:

package inject_mocks_test;
public class Subject {

    private final Section section;

    public Subject(Section section) {
        this.section = section;
    }

    public Section getSection() {
        return section;
    }
}

Section.java:

Section.java:

package inject_mocks_test;

public class Section {}

SubjectTest.java

SubjectTest.java

package inject_mocks_test;

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

public class SubjectTest {

    @Mock
    Section section;

    @InjectMocks
    Subject subject;

    @BeforeMethod
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void test1() {
        assertEquals(section, subject.getSection());
    }

    @Test
    public void test2() {
        assertEquals(section, subject.getSection());        
    }
}

干杯.

推荐答案

您正在使用 @InjectMocks 进行 constructor 注入.只要 Mockito 找到未初始化的字段(空),这将起作用.JUnit 在每次测试之前都会创建一个新的测试类实例,所以 JUnit 粉丝(像我一样)永远不会遇到这样的问题.TestNg 没有创建测试类的新实例.它保持测试方法之间的状态,所以当 MockitoAnnotations.initMocks(this) 第二次调用时,Mockito 会发现 subject 字段已经初始化并尝试使用 <强>场注入.这在另一回合将一直有效,直到该领域不是最终的.

You are using the @InjectMocks for constructor incjection. This will work as long as Mockito finds the field not initalized (null). JUnit is creating a new instance of the test class before each test, so JUnit fans (like me) will never face such problem. TestNg is not creating a new instance of test class. It's keeping the state between test methods, so when MockitoAnnotations.initMocks(this) is called for the second time, Mockito will find subject field already initialized and will try to use field injection. This on the other turn will work until the field is not final.

这是一个错误吗?我相信不是——而是 API 设计的自然结果.一些解决方法是添加

Is this is a bug? I believe not - rather a natural consequence of the API design. Some workaround for you would be to add

this.subject = null;

在一些 @AfterMethod 方法中.

这篇关于Mockito,@InjectMocks 最终字段的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending a keyboard event from java to any application (on-screen-keyboard)(将键盘事件从 java 发送到任何应用程序(屏幕键盘))
How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard(使用键盘滚动其弹出列表时如何使 JComboBox 所选项目不更改)
Capturing keystrokes without focus(在没有焦点的情况下捕获击键)
How can I position a layout right above the android on-screen keyboard?(如何将布局放置在 android 屏幕键盘的正上方?)
How to check for key being held down on startup in Java(如何检查在Java中启动时按住的键)
Android - Get keyboard key press(Android - 获取键盘按键)