模拟方法将异常包装在集合中而不是抛出异常
Mocked method wraps exception in collection rather than throwing exception
我有一个使用 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
返回的对象是一个集合,里面有异常
这是一个错误还是有任何解决方法?
从jmockit 1.34升级到最新版本。
我有一个使用 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
返回的对象是一个集合,里面有异常
这是一个错误还是有任何解决方法?
从jmockit 1.34升级到最新版本。