Mokito lying outside the test method

I have the following method outside the testing method

private DynamicBuild getSkippedBuild() { DynamicBuild build = mock(DynamicBuild.class); when(build.isSkipped()).thenReturn(true); return build; } 

but when I call this method, I get the following error

 org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here: -> at LINE BEING CALLED FROM Eg thenReturn() may be missing. Examples of correct stubbing: when(mock.isOk()).thenReturn(true); when(mock.isOk()).thenThrow(exception); doThrow(exception).when(mock).someVoidMethod(); Hints: 1. missing thenReturn() 2. you are trying to stub a final method, you naughty developer! 

It seems that mockito was not happy when you drowned out of the testing method. Is this not supported?

EDIT: I can get this to work by doing stubbing in the @Test method, but I want to reuse stubbing through @Test s.

+6
source share
1 answer

If isSkipped() not a final method, this problem probably indicates that you are trying to stub the method while the other method is stabilizing. It is not supported because Mockito uses the order of method calls ( when() , etc.) in its API.

I think you have something like this in your testing method:

 when(...).thenReturn(getSkippedBuild()); 

If so, you need to rewrite it as follows:

 DynamicBuild build = getSkippedBuild(); when(...).thenReturn(build); 
+13
source

Source: https://habr.com/ru/post/957452/


All Articles