Optional是否级联?
Is Optional cascaded?
级联模拟 return Optional
时的行为是什么?我的期望是 the returned Optional
object is also a mock.
然而,下面的测试表明情况并非如此:
@RunWith(JMockit.class)
public class CascadingTest {
public static interface Foo {
Optional<Bar> getOptionalBar();
}
public static interface Bar {
}
@Test
public void cascadingOptional(@Mocked final Foo foo) {
final Optional<Bar> opt = foo.getOptionalBar();
assertThat(opt.get(), is(not(nullValue())));
}
}
cascadingOptional
测试由于opt.get()
抛出的异常:
java.util.NoSuchElementException: No value present
at java.util.Optional.get(Optional.java:135)
at jmockit.CascadingTest.cascadingOptional(CascadingTest.java:39)
这似乎表明 opt
不是模拟。顺便说一句,如何(直接)检查对象是否是 JMockit
mock?
谢谢
JMockit
v1.17
mocking API documentation 根据它们的 return 类型指定默认情况下从模拟方法中 return 编辑的内容:
If no result is recorded for a given expectation, then all matching invocations will return the appropriate default value according to the
method return type:
- Most java.lang types (String, Object, etc.): returns null.
- java.math types (BigDecimal, etc.): returns null.
- Primitive/wrapper types: returns the standard default value (false for boolean/Boolean, 0 for int/Integer, and so on).
- java.util.List, java.util.Collection, or java.lang.Iterable: returns Collections.EMPTY_LIST.
- java.util.Iterator or java.util.ListIterator: returns an empty iterator.
- java.util.Set: returns Collections.EMPTY_SET.
- java.util.SortedSet: returns an unmodifiable empty sorted set.
- java.util.Map: returns Collections.EMPTY_MAP.
- java.util.SortedMap: returns an unmodifiable empty sorted map.
- java.util.Optional: returns Optional.empty().
- Other reference types: returns a mocked instance through cascading.
- Array types: returns an array with zero elements (empty) in each dimension.
级联模拟 return Optional
时的行为是什么?我的期望是 the returned Optional
object is also a mock.
然而,下面的测试表明情况并非如此:
@RunWith(JMockit.class)
public class CascadingTest {
public static interface Foo {
Optional<Bar> getOptionalBar();
}
public static interface Bar {
}
@Test
public void cascadingOptional(@Mocked final Foo foo) {
final Optional<Bar> opt = foo.getOptionalBar();
assertThat(opt.get(), is(not(nullValue())));
}
}
cascadingOptional
测试由于opt.get()
抛出的异常:
java.util.NoSuchElementException: No value present
at java.util.Optional.get(Optional.java:135)
at jmockit.CascadingTest.cascadingOptional(CascadingTest.java:39)
这似乎表明 opt
不是模拟。顺便说一句,如何(直接)检查对象是否是 JMockit
mock?
谢谢
JMockit
v1.17
mocking API documentation 根据它们的 return 类型指定默认情况下从模拟方法中 return 编辑的内容:
If no result is recorded for a given expectation, then all matching invocations will return the appropriate default value according to the method return type:
- Most java.lang types (String, Object, etc.): returns null.
- java.math types (BigDecimal, etc.): returns null.
- Primitive/wrapper types: returns the standard default value (false for boolean/Boolean, 0 for int/Integer, and so on).
- java.util.List, java.util.Collection, or java.lang.Iterable: returns Collections.EMPTY_LIST.
- java.util.Iterator or java.util.ListIterator: returns an empty iterator.
- java.util.Set: returns Collections.EMPTY_SET.
- java.util.SortedSet: returns an unmodifiable empty sorted set.
- java.util.Map: returns Collections.EMPTY_MAP.
- java.util.SortedMap: returns an unmodifiable empty sorted map.
- java.util.Optional: returns Optional.empty().
- Other reference types: returns a mocked instance through cascading.
- Array types: returns an array with zero elements (empty) in each dimension.