![](/img/trans.png)
[英]In jmockit, how can I mock a void method to throw an Exception on the first call and not to on subsequent calls?
[英]How can I mock a void method exception using JMockit
我在嘗試模擬void方法異常時遇到問題。
@Test
public void throwInternalServerException() throws Exception {
new NonStrictExpectations() {{
mockedClassInstance.voidMethod("abc", "def");
result = new ServiceException();
}};
mockMvc.perform(post("/someRestApi/"))
.andExpect(status().isInternalServerError());
在上面的代碼中,如果我用anyString替換params “abc”和“def” ,它的成功
我嘗試了這個問題的解決方案,但它不起作用。
有解決方案嗎
我使用了庫:jmockit和spring-test。
在場景中使用關鍵字anystring
,當任何參數(在這種情況下,'abc'和'def')時,它將拋出異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.