Mockito code during @Before annoted method solution
i'm new on using Mockito framework (and Behavior/Test Driven Development) and using to help me code some things;
I´m coding my own Java library for xml to create graphic things and
i tried first verify some behaviours:
(MockitoAnnotations.initMocks(Class)
is used in BaseTestClass
):
public class GeneralXml extends BaseTestClass {
private static final String TAG = "General XML";
@Mock(name="xml") private XmlGraphics mockXML;
@Before
public void s开发者_如何学编程etUp() throws Exception{
//stub method parseDimension
when(mockXML.parseDimension(anyString()))
.thenReturn(500);
// stub method parsePosition: 500 X 500 X 500 is an
// arbitrary dimension of window
when(mockXML.parsePosition(anyString()))
.thenReturn(
new Random().nextFloat() * mockXML.parseDimension(Xml.WIDTH),
new Random().nextFloat() * mockXML.parseDimension(Xml.HEIGHT),
new Random().nextFloat() * mockXML.parseDimension(Xml.DEPHT));
// Now Colors must have betwwen 0 and 255
// (RGB color)
when(mockXML.parseColorComponent(anyString()))
.thenReturn(
new Random().nextInt(256),
new Random().nextInt(256),
new Random().nextInt(256));
}
But I receive error in all subsequent verifications, like:
@Test
public void mockPosition() {
Log.d(TAG, "mock", "x = "+mockXML.parsePosition(Xml.X));
Log.d(TAG, "mock", "y = "+mockXML.parsePosition(Xml.Y));
Log.d(TAG, "mock", "z = "+mockXML.parsePosition(Xml.Z));
verify(mockXML, times(3)).parsePosition(anyString());
}
I think that my error is in:
when(mockXML.parsePosition(anyString()))
.thenReturn(
new Random().nextFloat() * mockXML.parseDimension(Xml.WIDTH),
new Random().nextFloat() * mockXML.parseDimension(Xml.HEIGHT),
new Random().nextFloat() * mockXML.parseDimension(Xml.DEPHT));
This is not a valid code? or I misunderstanding something? Thanks for any help
Try adding this as the first line in your setUp() function:
MockitoAnnotations.initMocks(this);
精彩评论