久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用@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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
Java mock database connection(Java 模擬數據庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設置為模擬對象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 久久看片 | 99久久精品免费看国产小宝寻花 | 精品视频一区二区三区在线观看 | 亚洲成av | 日本理论片好看理论片 | 亚洲综合区 | 中文日韩在线视频 | 中文字幕精品视频 | 国产成人精品视频 | 久久精品国产亚洲一区二区 | 久草新在线 | 成人在线播放网址 | www.99热这里只有精品 | 欧美中文字幕一区 | av二区三区| 免费午夜视频在线观看 | 一级片aaa | 国产欧美一区二区三区在线看 | 一级大黄色片 | 午夜视频在线播放 | 日韩中文一区 | 国产丝袜一区二区三区免费视频 | 成人久久久| 日日夜夜免费精品视频 | av成人在线观看 | 久久99精品视频 | 久久精品成人 | 久久久不卡网国产精品一区 | 国产精品区二区三区日本 | 亚洲精品欧美精品 | 久久精品99 | 国产一区在线看 | 久草视频网站 | 亚洲精品在线播放 | 蜜桃一区| 国产精品美女在线观看 | 中文字幕日韩三级 | 男人天堂国产 | 99国内精品久久久久久久 | 成人亚洲网站 | 91视频在线观看 |