You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
thenAnswer does not work with a mocked object created inline in a method.
See below, do you think this should work?
@Test
public void doesNotWork() {
ToyFactory toyFactory = mock(ToyFactory.class);
when(toyFactory.create()).thenReturn(createToy()); // DOES NOT WORK
}
@Test
public void works1() {
ToyFactory toyFactory = mock(ToyFactory.class);
when(toyFactory.create()).thenAnswer(x -> createToy()); // WORKS
}
@Test
public void works2() {
ToyFactory toyFactory = mock(ToyFactory.class);
Toy toyVariable = createToy();
when(toyFactory.create()).thenReturn(toyVariable); // WORKS
}
public Toy createToy() {
Toy toy = mock(Toy.class);
when(toy.getName()).thenReturn("Boy Toy");
return toy;
}
public interface Toy {
String getName();
}
public interface ToyFactory {
Toy create();
}
The text was updated successfully, but these errors were encountered:
thenAnswer
does not work with a mocked object created inline in a method.See below, do you think this should work?
The text was updated successfully, but these errors were encountered: