使用@Mock 和@InjectMocks

Using @Mock and @InjectMocks(使用@Mock 和@InjectMocks)
本文介绍了使用@Mock 和@InjectMocks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我目前正在研究 Mockito 框架,并使用 Mockito 创建了几个测试用例.但后来我读到,我可以使用 @Mock@InjectMocks 而不是调用 mock(SomeClass.class) - 我唯一的需要做的是用 @RunWith(MockitoJUnitRunner.class) 注释我的测试类或使用 MockitoAnnotations.initMocks(this);@Before方法.

I'm currently studying the Mockito framework and I've created several test cases using Mockito. But then I read that instead of invoking mock(SomeClass.class) I can use the @Mock and the @InjectMocks - The only thing I need to do is to annotate my test class with @RunWith(MockitoJUnitRunner.class) or use the MockitoAnnotations.initMocks(this); in the @Before method.

但它不起作用 - @Mock 似乎不起作用!这是我的 2 个代码修订版 - 一个使用注释,一个没有.

But it doesn't work - It seems that the @Mock won't work! Here is my 2 codes revisions - one using the annotations and one without.

我做错了什么?

public class ReportServiceImplTestMockito {

    private TaskService       mockTaskService; // This is the Mock object
    private ReportServiceImpl service;

    @Before
    public void init(){
        service         = new ReportServiceImpl();
        mockTaskService = mock(TaskServiceImpl.class);
        service.setTaskServiceImpl(mockTaskService);
    }
/// ...

 Some tests
}

正如我所说 - 这工作很棒.但以下不会:

As I said - this work great. But the following wont:

@RunWith(MockitoJUnitRunner.class)
public class ReportServiceImplTestMockito {

     @Mock 
     private TaskService      mockTaskService;

     @InjectMocks 
     private ReportServiceImpl service;

         // Some tests
}

这里是 ReportServiceImpl 类:

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private TaskService taskServiceImpl;

    public ReportServiceImpl(){}

    public ReportServiceImpl(TaskService taskService){
        this.taskServiceImpl = taskService;
    }

    public void setTaskServiceImpl(TaskService taskServiceImpl) {
        this.taskServiceImpl = taskServiceImpl;
    }
}

我错过了什么?

推荐答案

好吧,我搞错了!!!我使用了 @InjectMocks 但在 init() 方法中初始化了相同的变量...所以发生的事情是,mockito 将模拟对象注入到我的变量中 - 但几秒钟后我运行了它 - 初始化了同一个变量!!!

O.K, I got my mistake!!! I've used the @InjectMocks but initialized the same variable in the init() method... So what happened was that mockito injected the mock objects to my variable - but seconds later I ran it over - initializing that very same variable!!!

这篇关于使用@Mock 和@InjectMocks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)