Mocked方法在集合中包装异常而不是抛出异常

时间:2018-06-01 20:48:08

标签: java junit jmockit

我使用失败的jmockit's result = new Object[] {...}进行了测试。 jmockit版本是1.34。测试应抛出异常,但jmockit返回一个带有异常的集合。这是一个例子:

public class ServiceTest {
    public class Service {
        private Set<String> saved;

        public Service() {
            saved = new HashSet<>();
            saved.add("one");
            saved.add("two");
        }

        public Set<String> readAll() {
            return Collections.unmodifiableSet(saved);
        }
    }

    @Test(expected = RuntimeException.class)
    public void testReadAll(@Mocked Service service) {
        new Expectations() {{
                service.readAll(); times = 1; result = new RuntimeException();
        }};

        service.readAll();
    }

    @Test
    public void testReadAllWithArray(@Mocked Service service) {
        new Expectations() {{
                service.readAll(); times = 1; result = new Object[]{new RuntimeException()};
        }};

        Set set = service.readAll();
        assertThat(set.iterator().next(), instanceOf(RuntimeException.class));
    }
}

testReadAllWithArray显示readAll返回的对象是一个包含异常的集合。

这是一个错误还是有任何解决方法?

1 个答案:

答案 0 :(得分:1)

从jmockit 1.34升级到最新版本。

相关问题