Created
April 11, 2012 07:49
-
-
Save clakech/2357686 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Yes of course.
I just want to understand why this does not work using @nnotations ;-)
Thanks,
Cyril Lakech
0622793199
Le 11 avril 2012 10:38, Frdric Menou <
reply@reply.github.com
… a crit :
Does it work if you manually spy familyController like this:
@before
public void setup() {
familyController = Mockito.spy(new FamilyController(familyService));
}
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/2357686
This is probably a matter of fields ordering in compiled class ; I've observed subtle changes on that topic since jdk7
Oh really... sh**! Tks btw
You can use Annotations together introducing @Injectmocks (see my fork : https://gist.github.com/2358140)
documentation of @Injectmocks and the way it injects @Mocks into @SPY objets or not-mockito objects http://docs.mockito.googlecode.com/hg/1.9.0/org/mockito/InjectMocks.html
Sounds great, Thank you !
I didn't know I can use those 2 annotations on the same object.
Regards,
Cyril Lakech
2012/4/11 Ludovic Meurillon <
reply@reply.github.com
… You can use Annotations together introducing @Injectmocks (see my fork :
https://gist.github.com/2358140)
documentation of @Injectmocks and the way it inject @Mocks into @SPY or
not-mockito objects
http://docs.mockito.googlecode.com/hg/1.9.0/org/mockito/InjectMocks.html
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/2357686
Good to know indeed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does it work if you manually spy familyController like this:
@before
public void setup() {
familyController = Mockito.spy(new FamilyController(familyService));
}