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

Mockito 通過但代碼覆蓋率仍然很低

Mockito Passes but Code Coverage still low(Mockito 通過但代碼覆蓋率仍然很低)
本文介紹了Mockito 通過但代碼覆蓋率仍然很低的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

package com.fitaxis.test;

import java.sql.SQLException;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import static org.mockito.Mockito.*;

import com.fitaxis.leaderboard.LeaderBoard;

public class LeaderBoardTests {


 @Test 
 public void TestThatDataIsSavedToTheDatabase()
 {
  LeaderBoard leaderBoard = mock(LeaderBoard.class);
  //doNothing().doThrow(new RuntimeException()).when(leaderBoard).saveData();

  when(leaderBoard.saveData()).thenReturn(true);

  boolean res = leaderBoard.saveData();

  verify(leaderBoard).saveData();

  Assert.assertTrue(res);
 }

}

我已經使用 mockito 來模擬一個類,但是當我使用代碼覆蓋率時,它不會檢測到該方法已被調用.難道我做錯了什么?請幫忙!

I have used mockito to mock a class, but when I use code coverage it does not detect that the method as been called. Am I doing something wrong? Please help!

推薦答案

看起來你正在模擬你對生產代碼的唯一調用.

It looks like you're mocking out the only call you're making to production code.

換句話說,你的測試說:

In other words, your test says:

  • 當我調用 saveData() 時,偽造結果返回 true
  • 現在調用 saveData() - 是的,結果是真的!
  • When I call saveData(), fake the result to return true
  • Now call saveData() - yay, the result was true!

據我所知,您的生產代碼根本沒有被調用.

None of your production code is being calls at all, as far as I can see.

模擬的目的是從生產類中模擬出依賴項,或者(有時,盡管我不喜歡)模擬出您實際測試的代碼將調用的生產類的一些方法.

The point of mocking is to mock out dependencies from your production class, or (sometimes, though I prefer not to) to mock out some methods of your production class that the code you're actually testing will call.

您應該可能模擬出 Leaderboard 的依賴關系,而不是 Leaderboard 本身.如果你必須模擬出saveData(),你應該測試調用 saveData()的方法.. 檢查它們是否保存了正確的數據,當 saveData() 返回 false 時它們是否正確運行,等等.

You should probably be mocking out the dependencies of Leaderboard rather than Leaderboard itself. If you must mock out saveData(), you should be testing the methods that call saveData()... check that they save the right data, that they act correctly when saveData() returns false, etc.

這篇關于Mockito 通過但代碼覆蓋率仍然很低的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 可以免费看av的网站 | 91亚洲国产 | aaa级片 | 一区中文字幕 | 日韩精品视频免费在线观看 | 欧美高清在线 | 91视频免费观看 | 日韩精品第一页 | 三上悠亚激情av一区二区三区 | 精品在线免费观看 | 日产精品久久久一区二区 | 日韩特黄 | 亚洲国产精品久久久 | 天堂av资源 | 久久久久精 | 99视频网站 | 亚洲视频免费 | 亚洲 欧美 日韩 在线 | 欧美天堂 | 日韩精品在线免费观看 | 综合av网| 美日韩一区二区三区 | 亚洲成人免费观看 | 成人精品影院 | 在线观看欧美日韩视频 | 欧美在线日韩 | 久操av在线| 精品黑人一区二区三区国语馆 | 看黄网站在线观看 | 亚洲精品免费在线 | 黄色网址在线播放 | 成人午夜在线观看 | av免费网站 | av大片在线观看 | 日产精品久久久一区二区 | 中文字幕理伦片免费看 | 日本三级大片 | 国产乱码一区二区三区 | 国产福利小视频 | 婷婷综合五月 | 一级特黄aaaaaa大片 |