Почему слежка за экземпляром с использованием mockito не работает должным образом - PullRequest
0 голосов
/ 12 апреля 2020

Я высмеиваю служебный слой и заглушаю один из методов, но doReturn не возвращает смоделированное значение. Ниже приведен мой код, что я делаю неправильно

 public class Service
 {
   @Autowired
    Dao dao;
   ....
   public int doSomething(){
      return 2;
   }
 }

@Category(IntegrationTest.class)
@RunWith(SpringRunner.class)
public class Test{

   @Autowired
   Service instanceOfService;

  @Test
  public void testMethod(){

   Service mockedService = Mockito.spy(instanceOfService)
   Mockito.doReturn(4).when(mockedService).doSomething();
   Assert.assertEquals(4, mockedService.doSomething());

    }
}

здесь утверждение не выполняется, говоря, что ожидаемое значение равно <4>, но было <2>

, если я изменил doReturn на это

Mockito.doReturn(4).when(mockedService.doSomething()).

затем выдает это исключение:

org.mockito.exceptions.misusing.NotAMockException: Argument passed to when() is not a mock! Example of correct stubbing: doThrow(new RuntimeException()).when(mock).someMethod();

, а также, если я изменю его на

Mockito.when(mockedService.doSomething()).thenReturn(4);

it выдает эту ошибку:

org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles); Also, this error might show up because: 1. you stub either of: final/private/equals()/hashCode() methods. Those methods *cannot* be stubbed/verified. Mocking methods declared on non-public parent classes is not supported. 2. inside when() you don't call method on mock but on some other object.

...