-1

When I try to run the following test

public class FavoriteServiceTest extends AbstractCoreTest {

    @Autowired
    private FavoriteRepository favoriteRepository;

    @Autowired
    private RevisionService revisionService;

    @Autowired
    private FavoriteService favoriteService;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        when(revisionService.getGlobalRevisionNumber()).thenReturn(1L);
    }

    @Test
    public void loadFavorites() throws Exception {
        when(favoriteRepository.findFavoritesByUserId("123")).thenReturn(Collections.emptyList());
        List<Favorite> favorites = favoriteService.loadFavorites(123L);

        assertThat(favorites.size(), is(0));
    }

I get the following exception, but im pretty sure the mock is correct initialized

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.

at FavoriteServiceTest.setUp(FavoriteServiceTest.java:44) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)

MemLeak
  • 3,865
  • 4
  • 39
  • 77
  • check the below link it will be help you.. http://stackoverflow.com/questions/9186604/mockito-exception-when-requires-an-argument-which-has-to-be-a-method-call-on – kaushik Sep 13 '16 at 08:46

2 Answers2

0

Just replace @Autowired with @Mock -.-

@Mock
private DocumentService documentService;
MemLeak
  • 3,865
  • 4
  • 39
  • 77
0

Your favoriteRepository should be a mock object, benfinit from spring boot, you can using @MockBean here.

Liping Huang
  • 3,794
  • 3
  • 23
  • 39