Java測(cè)試框架推薦

  • Java測(cè)試框架介紹
    java有很多測(cè)試類框架, 開(kāi)發(fā)中有很多比如Mokito, powermock, wiremock, cucumber ,但是powermock測(cè)試,sonar不認(rèn)其覆蓋率.

Mockito
What is mock
Mocking is primarily used in unit testing. An object under test may have dependencies on other (complex) objects. To isolate the behavior of the object you want to replace the other objects by mocks that simulate the behavior of the real objects. This is useful if the real objects are impractical to incorporate into the unit test.

From https://stackoverflow.com/questions/2665812/what-is-mocking

以下所有的例子以下圖為依據(jù)皆辽, 寫UserImp的UT UserImplTest

Import mockito
//build.gradlew
repositories { jcenter() }
dependencies {
testCompile "org.mockito:mockito-core:2.+"
// https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.2'

}

// 遇到以下錯(cuò)誤需要導(dǎo)入

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)

at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:74)
at com.sun.proxy.$Proxy8.isTypeMockable(Unknown Source)
at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:240)
at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:228)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:61)
at org.mockito.Mockito.mock(Mockito.java:1908)

Mock and injectMock
@Mock 用來(lái)mock 獨(dú)立沒(méi)有依賴的類
@InjectMock 用于去mock有依賴的類
For dependent classes, we used mocks.
From https://howtodoinjava.com/mockito/mockito-mock-injectmocks/

import com.kaifei.model.User;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.List;

public class UserImplTest {

@Mock
UserHandler userHandler;

@InjectMocks
UserImpl userImpl;

@Before
public void before()
{
    //Initializes objects annotated with Mockito annotations for given testClass: @Mock, @Spy, @Captor, @InjectMocks
    //See examples in javadoc for MockitoAnnotations class.
    MockitoAnnotations.initMocks(this);
}

@Test
public void testGetAllUser()
{
    //given
    User james = new User("james", 23);
    ArrayList<User> objects = new ArrayList<>();
    objects.add(james);
    Mockito.when(userHandler.getAll()).thenReturn(objects);

    //when
    List<User> actUsers = userImpl.getaAllUsers();

    //then
    Assert.assertEquals("get all done!", objects, actUsers);
}

}
mock方法的返回值
Mockito.when(sharingDataHandler.createSharingData()).thenReturn(sharingUuid);
Mock方法被執(zhí)行了一次
用來(lái)測(cè)void方法

@Test
public void testCreateUser200()
{
    //given
    User james = new User("james", 23);

    //when
    userImpl.creatUser(james);

    //then verify the UserHandler::createUser was executed only once
    Mockito.verify(userHandler, Mockito.times(1)).createUser(james);
}

Mock throw exception
@Test(expected = NullPointerException.class)
public void testCreatUser()
{
//given
User james = new User("james", 23);

    //when
    userImpl.creatUser(james);
    
    //then
    //catch NullPointerException exception
}

RabbitMQ clear Message
@Autowired
private MessageCollector messageCollector;
@After("@CleanAllMessages")
public final void cleanAllMessages()
{
try
{
mockMvc.perform(
delete("/messages/").contentType(MediaType.APPLICATION_JSON_UTF8_VALUE).content("[]"));
messageCollector.forChannel(messageSource.statusmessageChannel()).clear();
}
catch (Exception e)
{
throw new CucumberException("Stopped at "user invokes root cleanAllMessages:"", e);
}
}
Cucumber
cucum是BDD測(cè)試框架的一個(gè)工具, 能夠測(cè)試組件與組件之間的API調(diào)用, service里API的測(cè)試

https://cloud.tencent.com/developer/article/1628939

WireMock
Mock your APIs for fast, robust and comprehensive testing
WireMock is a simulator for HTTP-based APIs. Some might consider it a service virtualization tool or a mock server.

It enables you to stay productive when an API you depend on doesn't exist or isn't complete. It supports testing of edge cases and failure modes that the real API won't reliably produce. And because it's fast it can reduce your build time from hours down to minutes.
wireMock site
http://wiremock.org/

本文由博客群發(fā)一文多發(fā)等運(yùn)營(yíng)工具平臺(tái) OpenWrite 發(fā)布

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市涧狮,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌扯夭,老刑警劉巖械媒,帶你破解...
    沈念sama閱讀 211,290評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異限府,居然都是意外死亡本昏,警方通過(guò)查閱死者的電腦和手機(jī)供汛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,107評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)涌穆,“玉大人怔昨,你說(shuō)我怎么就攤上這事∷尴。” “怎么了趁舀?”我有些...
    開(kāi)封第一講書人閱讀 156,872評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)祝沸。 經(jīng)常有香客問(wèn)我矮烹,道長(zhǎng)越庇,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 56,415評(píng)論 1 283
  • 正文 為了忘掉前任奉狈,我火速辦了婚禮卤唉,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘仁期。我一直安慰自己桑驱,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,453評(píng)論 6 385
  • 文/花漫 我一把揭開(kāi)白布跛蛋。 她就那樣靜靜地躺著熬的,像睡著了一般。 火紅的嫁衣襯著肌膚如雪赊级。 梳的紋絲不亂的頭發(fā)上押框,一...
    開(kāi)封第一講書人閱讀 49,784評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音此衅,去河邊找鬼强戴。 笑死,一個(gè)胖子當(dāng)著我的面吹牛挡鞍,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播预烙,決...
    沈念sama閱讀 38,927評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼墨微,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了扁掸?” 一聲冷哼從身側(cè)響起翘县,我...
    開(kāi)封第一講書人閱讀 37,691評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎谴分,沒(méi)想到半個(gè)月后锈麸,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,137評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡牺蹄,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,472評(píng)論 2 326
  • 正文 我和宋清朗相戀三年忘伞,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片沙兰。...
    茶點(diǎn)故事閱讀 38,622評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡氓奈,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出鼎天,到底是詐尸還是另有隱情舀奶,我是刑警寧澤,帶...
    沈念sama閱讀 34,289評(píng)論 4 329
  • 正文 年R本政府宣布斋射,位于F島的核電站育勺,受9級(jí)特大地震影響但荤,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜涧至,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,887評(píng)論 3 312
  • 文/蒙蒙 一腹躁、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧化借,春花似錦潜慎、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 30,741評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至蒜焊,卻和暖如春倒信,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背泳梆。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工鳖悠, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人优妙。 一個(gè)月前我還...
    沈念sama閱讀 46,316評(píng)論 2 360
  • 正文 我出身青樓乘综,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親套硼。 傳聞我的和親對(duì)象是個(gè)殘疾皇子卡辰,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,490評(píng)論 2 348