我正在努力学习JMockit的各个方面。这里是另一个我不理解的JMockit怪异之处的例子。使用NonStrictExpectations运行测试完全正常。然而,使用MockUp运行测试不行。我不确定原因。有任何想法吗?我正在运行JMockit 1.5。
要测试的方法:
成功期望测试
要测试的方法:
private List<Foo> getFooList(List<FooStatement> fooStatements){
List<Foo> FooList = new ArrayList<Foo>();
for(FooStatement at: fooStatements){
List<Foo> aList = at.getFoos();
FooList.addAll(aList);
}
return FooList;
}
成功期望测试
@Test
public void getFooListWithExpectationsTest(
@Mocked final FooStatement mockFooStatement,
@Mocked final Foo mockFoo
){
List<FooStatement> fooStatementList = new ArrayList<>(Arrays.asList(
mockFooStatement,
mockFooStatement
));
new NonStrictExpectations(){{
mockFooStatement.getFoos();
result = new ArrayList<Foo>(Arrays.asList(mockFoo));
}};
List<Foo> fooList = Deencapsulation.invoke(handler, "getFooList", fooStatementList);
Assert.assertTrue(fooList.size() == 2);
}
使用MockUp时出现断言错误(0 != 2)
@Test
public void getFooListWithMockUpTest(
@Mocked final FooStatement mockFooStatement,
@Mocked final Foo mockFoo
){
new MockUp<FooStatement>(){
@Mock
public List<Foo> getFoos(){
return new ArrayList<Foo>(Arrays.asList(mockFoo));
}
};
List<FooStatement> fooStatementList = new ArrayList<>(Arrays.asList(
mockFooStatement,
mockFooStatement
));
List<Foo> fooList = Deencapsulation.invoke(handler, "getFooList", fooStatementList);
Assert.assertTrue(fooList.size() == 2);
}