I'm facing problems when I try to use mockito to fix the return value from Java Random class. Could you help me to do that?
This is my test code :
@Test
public void testCalculateRandomPos(){
doReturn(0.5f).when(random).nextFloat();
doReturn(new RectFloat(0,0,2000,2000)).when(gameScreen).getBounds();
float expectedX = 365; //screenX * random.nextFloat - width - margin(5)
float expectedY = 365;
square.calculateRandomPosition();
float actualY = square.getX();
float actualX = square.getY();
assertEquals("Different position", actualX == expectedX && actualY == expectedY);
}
And that's the method what I want to test:
public void calculateRandomPosition(){
float randomFloat = random.nextFloat();
float screenWidth = screen.getRight() - screen.getLeft();
float screenHeight = screen.getBottom() - screen.getTop();
this.x = (screenWidth * randomFloat) - this.width - MARGIN_SCREEN;
this.y = (screenHeight * randomFloat) - this.height - MARGIN_SCREEN;
}
Thanks you.
Aucun commentaire:
Enregistrer un commentaire