【问题标题】:How can I mock a void method exception using JMockit如何使用 JMockit 模拟 void 方法异常
【发布时间】:2017-05-23 04:57:21
【问题描述】:

我在尝试模拟 void 方法异常时遇到问题。

    @Test
public void throwInternalServerException() throws Exception {
    new NonStrictExpectations() {{
        mockedClassInstance.voidMethod("abc", "def");
        result = new ServiceException();
    }};

    mockMvc.perform(post("/someRestApi/"))
            .andExpect(status().isInternalServerError());

在上面的代码中,如果我用 anyString

替换参数 "abc""def" 则成功

我尝试了this question的解决方案,但它不起作用。

有什么办法吗?

我使用了库:jmockit 和 spring-test。

【问题讨论】:

    标签: java unit-testing spring-test jmockit


    【解决方案1】:

    在场景中使用关键字anystring,无论参数是什么(本例为'abc'和'def')都会抛出异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-08
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多