單元測試JMockit使用

2021-09-11 09:18:42 字數 959 閱讀 9278

title: 單元測試jmockit使用 tags:

由於目前dubbo等外部依賴越來越多 現在小夥伴關於測試經常跑不通 比如

之前也提供了stub方案,但是目前使用的人幾乎沒有junit測試之第三方元件mock

場景需要返回特殊的值(特定異常特定值 非法值等等就ok)

@test

public void testjmockit()

}.getmockinstance();

assert.assertequals( mockinstance.get(1),"aaaa");

assert.assertequals( mockinstance.get(integer.max_value),"aaaa");

assert.assertequals( mockinstance.size(),0);

}複製**

注意此處為介面 呼叫了getmockinstance 非常神奇的返回了指定的值 並且模擬出了一種size為0但是get一直可以返回特定結果的情況

使用spring註解部分依賴特定的外部服務(此處需要依賴網路)

};assert.assertequals( carzonehttpclient.getjsonstring("",""),"我是mocked!");

}複製**

很明顯這樣就可以直接返回了需要的值

部分依賴dubbo介面

new mockup() };

複製**

此處業務邏輯可以強行跳過 這樣可以不必要依賴指定介面(dubbo,rmi)也可以製造空指標等測試**異常

嚴格來說基本上mockup可以完成絕大部分我能想到的測試場景了~

單元測試JMockit使用

title 單元測試jmockit使用 tags 由於目前dubbo等外部依賴越來越多 現在小夥伴關於測試經常跑不通 比如 之前也提供了stub方案,但是目前使用的人幾乎沒有junit測試之第三方元件mock 場景需要返回特殊的值 特定異常特定值 非法值等等就ok test public void ...

JMockit編寫java單元測試

jmockit例項 package test.mock.jmockit import static org.junit.assert.assertequals import mockit.deencapsulation import mockit.expectations import mockit...

Google Test單元測試使用

google開源了很多實用的模組,比如google gtest google gmock google glog google gflags google ctemplate google sparsehash protobuf perftools,gtest是c 的測試模組,提供豐富的測試方法 軟...