1.

Can I Thenreturn() An Inlined Mock() ?

Answer»

Unfortunately you cannot do this:

when(m.foo()).thenReturn(mock(Foo.class));
// ^

The reason is that detecting UNFINISHED stubbing wouldn't work if we allow above construct. We consider this as a 'trade off' of framework validation (see also previous FAQ entry). However you can slightly CHANGE the code to MAKE it working:

//extract local VARIABLE and start smiling:

Foo foo = mock(Foo.class);
when(m.foo()).thenReturn(foo);

Unfortunately you cannot do this:

when(m.foo()).thenReturn(mock(Foo.class));
// ^

The reason is that detecting unfinished stubbing wouldn't work if we allow above construct. We consider this as a 'trade off' of framework validation (see also previous FAQ entry). However you can slightly change the code to make it working:

//extract local variable and start smiling:

Foo foo = mock(Foo.class);
when(m.foo()).thenReturn(foo);



Discussion

No Comment Found