JMockit - Expectations vs MockUp<T> Why does one work and the other doesn't?
You are using MockUp<?>
incorrectly. MockUp<T?
will tell JMockit to redefine a classes loaded to JVM so that instead of the real class initialization of FooStatement
, it will replace them by the ones defined in the MockUp<FooStatement
.
So basically MockUp<FooStatement>
will automatically replace calls of new FooStatement()
.
Try something like:
@Test
public void getFooListWithMockUpTest(@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(
new FooStatement(),
new FooStatement()
));
List<Foo> fooList = Deencapsulation.invoke(handler, "getFooList", fooStatementList);
Assert.assertTrue(fooList.size() == 2);
}