Skip to content

Commit db2cdef

Browse files
author
jhonochoa
committed
Se crea clase Player y Dice y se realiza Tests de player haciendo uso de Mockito sobre Dice
1 parent 5d2b1c6 commit db2cdef

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

javatest/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,12 @@
1919
<version>4.12</version>
2020
<scope>test</scope>
2121
</dependency>
22+
<dependency>
23+
<groupId>org.mockito</groupId>
24+
<artifactId>mockito-core</artifactId>
25+
<version>2.23.4</version>
26+
<scope>test</scope>
27+
</dependency>
28+
2229
</dependencies>
2330
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
*
3+
*/
4+
package com.testing.javatest.player;
5+
6+
import java.util.Random;
7+
8+
/**
9+
* Clase que representa un dado que usa la clase {@link Player}
10+
*
11+
*
12+
* @author sumel
13+
*
14+
*/
15+
public class Dice {
16+
17+
private int sides;
18+
19+
public Dice(int sides) {
20+
21+
this.sides = sides;
22+
}
23+
24+
public int roll() {
25+
return new Random().nextInt(sides) + 1;
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
*
3+
*/
4+
package com.testing.javatest.player;
5+
6+
/**
7+
* Clase que representa un jugador
8+
*
9+
* @author sumel
10+
*
11+
*/
12+
public class Player {
13+
private Dice dice;
14+
private int mimNumberToWin;
15+
16+
public Player(Dice dice, int mimNumberToWin) {
17+
this.dice = dice;
18+
this.mimNumberToWin = mimNumberToWin;
19+
}
20+
21+
public boolean play() {
22+
int diceNumber = dice.roll();
23+
return diceNumber > mimNumberToWin;
24+
}
25+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
*
3+
*/
4+
package com.testing.javatest.player;
5+
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertFalse;
10+
import static org.junit.Assert.assertTrue;
11+
12+
import org.junit.Assert.*;
13+
import org.mockito.Mockito;
14+
15+
/**
16+
* Clase de test para la clase {@link Player}
17+
*
18+
* @author sumel
19+
*
20+
*/
21+
public class PlayerTest {
22+
23+
@Test
24+
public void lossesPlayerWhenIsTooLow() {
25+
//Dice dice = new Dice(6);
26+
Dice dice = Mockito.mock(Dice.class);
27+
Mockito.when(dice.roll()).thenReturn(2);
28+
29+
Player player = new Player(dice, 3);
30+
31+
//assertEquals(false, player.play());
32+
assertFalse(player.play());
33+
}
34+
35+
@Test
36+
public void WinPlayerWhenIsBig() {
37+
38+
Dice dice = Mockito.mock(Dice.class);
39+
Mockito.when(dice.roll()).thenReturn(4);
40+
41+
Player player = new Player(dice, 3);
42+
43+
//assertEquals(true, player.play());
44+
assertTrue(player.play());
45+
}
46+
}
47+

0 commit comments

Comments
 (0)