使用Mockito模拟异常


使用Mockito模拟异常,以MyDictionary类为例进行介绍

class MyDictionary {

    private Map<String, String> wordMap;

    MyDictionary() {
        wordMap = new HashMap<>();
    }

    public void add(final String word, final String meaning) {
        wordMap.put(word, meaning);
    }

    String getMeaning(final String word) {
        return wordMap.get(word);
    }
}

1 Mock对象模拟异常

1.1方法返回类型非void

如果方法的返回类型非void,那么可以使用when().thenThrow()来模拟异常:

    @Test(expected = NullPointerException.class)
    public void whenConfigNonVoidRetunMethodToThrowEx_thenExIsThrown() {
        MyDictionary dictMock = mock(MyDictionary.class);
        when(dictMock.getMeaning(anyString())).thenThrow(NullPointerException.class);

        dictMock.getMeaning("word");
    }

1.2方法返回类型是void

如果方法返回类型是void,则使用doThrow来模拟异常

@Test(expected = IllegalStateException.class)
public void whenConfigVoidRetunMethodToThrowEx_thenExIsThrown() {
    MyDictionary dictMock = mock(MyDictionary.class);
    doThrow(IllegalStateException.class)
      .when(dictMock)
      .add(anyString(), anyString());
 
    dictMock.add("word", "meaning");
}

1.3模拟异常对象

我们不仅可以在thenThrow()或者doThrow() 中模拟异常类,还可以模拟异常对象。

    @Test(expected = NullPointerException.class)
    public void whenConfigNonVoidRetunMethodToThrowExWithNewExObj_thenExIsThrown() {
        MyDictionary dictMock = mock(MyDictionary.class);
        when(dictMock.getMeaning(anyString())).thenThrow(new NullPointerException("Error occurred"));

        dictMock.getMeaning("word");
    }

    @Test(expected = IllegalStateException.class)
    public void whenConfigVoidRetunMethodToThrowExWithNewExObj_thenExIsThrown() {
        MyDictionary dictMock = mock(MyDictionary.class);
        doThrow(new IllegalStateException("Error occurred")).when(dictMock)
            .add(anyString(), anyString());

        dictMock.add("word", "meaning");
    }

2 Spy对象模拟异常

Spy对象模拟异常的方式与Mock对象相同,如下:

@Test(expected = NullPointerException.class)
public void givenSpy_whenConfigNonVoidRetunMethodToThrowEx_thenExIsThrown() {
    MyDictionary dict = new MyDictionary();
    MyDictionary spy = Mockito.spy(dict);
    when(spy.getMeaning(anyString()))
      .thenThrow(NullPointerException.class);
 
    spy.getMeaning("word");
}
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用Mockito模拟try-catch块的过程类似于模拟其他方法的过程。我们可以使用Mockito的when-then模式来模拟try块中的方法调用,并使用Mockito的doThrow模式来模拟catch块中的异常抛出。 首先,我们需要使用mock方法创建一个模拟对象,并将其用作try块中的对象。例如,我们可以使用下面的代码创建一个模拟的List对象: ``` List mockList = mock(List.class); ``` 然后,我们可以使用when方法模拟try块中的方法调用,并指定它们的返回值。例如,我们可以使用下面的代码模拟在try块中调用List的get方法并返回特定的值: ``` when(mockList.get(0)).thenReturn(1); ``` 接下来,我们可以使用doThrow方法模拟catch块中的异常抛出。例如,我们可以使用下面的代码模拟在catch块中抛出一个特定的异常: ``` doThrow(new RuntimeException()).when(mockList).add(any()); ``` 在这个例子中,我们使用doThrow方法模拟在调用List的add方法时抛出RuntimeException异常。 最后,我们可以在测试中使用模拟对象并捕获try块中的异常,以验证try-catch块的行为。例如,我们可以使用下面的代码在try块中调用List的get方法,并捕获可能抛出的异常: ``` try { int value = mockList.get(0); } catch (Exception e) { // 处理异常 } ``` 在这个例子中,如果模拟的List对象在调用get方法时抛出了异常,我们将捕获并处理它。 总结起来,使用Mockito模拟try-catch块的过程包括:创建模拟对象,模拟try块中的方法调用,并指定它们的返回值,模拟catch块中的异常抛出,使用模拟对象并捕获try块中的异常。 引用来源: https://www.cnblogs.com/EasonJim/p/7804453.html 引用来源: https://www.cnblogs.com/zixia/p/5736014.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值