Skip to content

Instantly share code, notes, and snippets.

@clakech
Created April 11, 2012 07:49
Show Gist options
  • Save clakech/2357686 to your computer and use it in GitHub Desktop.
Save clakech/2357686 to your computer and use it in GitHub Desktop.
Why Mockito @SPY and @mock doesn't work together ?
package com.bla.web.controller;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import com.bla.web.dto.FamilyAndItems;
import com.bla.web.service.FamilyServiceImpl;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyString;
import static org.fest.assertions.Assertions.assertThat;
@RunWith(MockitoJUnitRunner.class)
public class SpyAndMockFailTest {
@Mock
FamilyServiceImpl familyService;
@Spy
FamilyController familyController = new FamilyController(familyService);
@Test
public void shouldGetFamilyAndItems() {
// given
given(familyService.getFamilyAndItems(anyString())).willReturn(null);
// when
FamilyAndItems familyAndItems = familyController.getFamilyAndItems("id");
// then
assertThat(familyAndItems).isEqualTo(null);
}
}
@ptitfred
Copy link

Good to know indeed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment