1
1
package com .codedifferently .lesson16 ;
2
2
3
3
import static org .junit .jupiter .api .Assertions .assertEquals ;
4
- import static org .junit .jupiter .api .Assertions .assertFalse ;
5
4
import static org .junit .jupiter .api .Assertions .assertNotEquals ;
6
5
import static org .junit .jupiter .api .Assertions .assertThrows ;
6
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
7
+
8
+ import com .codedifferently .lesson16 .gumball .Colors ;
9
+ import org .junit .jupiter .api .BeforeEach ;
7
10
import org .junit .jupiter .api .Test ;
8
11
9
12
public class GumballMachineTest {
10
- GumBallMachine testGumBallMachine (20 , );
13
+ GumBallMachine testGumBallMachine ;
14
+
15
+ @ BeforeEach
16
+ void setUp () {
17
+ testGumBallMachine = new GumBallMachine (10 , false ); // Initialize with 10 gumballs
18
+ }
11
19
12
20
@ Test
13
- void testGumballMachineCount () {
14
- //arrange
15
- testGumBallMachine = new gumballMachine ();
16
- //act
17
- testGumballMachine .removeGumball ();
18
- //assert
19
- assertNotEquals (GumballmachineCount .getGumballCount (),testGumballMachine );
20
- }
21
+ void testGumBallCount_afterDispense () throws invalidCoinInsertedException {
22
+ int initialCount = testGumBallMachine .getGumBallCount ();
23
+ testGumBallMachine .getGumBall (0.25 ); // Dispense a gumball
24
+ assertEquals (initialCount - 1 , testGumBallMachine .getGumBallCount ());
25
+ }
21
26
22
- @ Test
23
- void testGetGumBall () {
24
- // Arrange
25
- var quater = 0.25 ;
26
- var previousGumBallCount = gumBallMachine .getGumBallCount ();
27
- // Act
28
- gumballmachine .getGumBall (quater );
27
+ @ Test
28
+ void testSetRandomColor () {
29
+ Colors previousColor = testGumBallMachine .getCurrentColor ();
30
+ Colors newColor = previousColor ;
31
+ int attempts = 0 ;
32
+ int maxAttempts = 10 ; // Set a limit for attempts
29
33
30
- // Assert
31
- assertNotEquals (gumballmachine .getGumBallCount (),previousGumBallCount );
34
+ // Try to change the color until it is different or max attempts reached
35
+ while (newColor .equals (previousColor ) && attempts < maxAttempts ) {
36
+ testGumBallMachine .setRandomColors (); // Set a new random color
37
+ newColor = testGumBallMachine .getCurrentColor ();
38
+ attempts ++;
32
39
}
33
- @ Test
34
- testGumballMachineIsEmpty_whenEmpty () {
35
- // Arrange
36
-
37
40
38
- // Act
39
- testGumballMachine .getGumBall (threedollars );
41
+ assertNotEquals (
42
+ previousColor , newColor , "The color should have changed after multiple attempts" );
43
+ }
40
44
41
- // Assert
42
- assertEquals ("No more gum! Refill." , testGumballMachine .IsEmpty ());
45
+ @ Test
46
+ void testGetGumBall_whenValidQuarter () throws invalidCoinInsertedException {
47
+ int previousCount = testGumBallMachine .getGumBallCount ();
48
+ testGumBallMachine .getGumBall (0.25 );
49
+ assertEquals (previousCount - 1 , testGumBallMachine .getGumBallCount ());
50
+ }
51
+
52
+ @ Test
53
+ void testGetGumBall_whenMachineIsEmpty () throws invalidCoinInsertedException {
54
+ for (int i = 0 ; i < 10 ; i ++) {
55
+ testGumBallMachine .getGumBall (0.25 ); // Dispense all gumballs
43
56
}
44
- @ Test
45
- testGetGumBall_InvalidCoinPenny () {
46
-
47
- assertThrows (invalidCoinInsertedException .class ,()->{
48
- testGumBallMachine .getGumBall (0.01 );
57
+ assertThrows (
58
+ invalidCoinInsertedException .class ,
59
+ () -> {
60
+ testGumBallMachine .getGumBall (0.25 ); // Try to dispense from an empty machine
49
61
});
50
- }
51
- @ Test
52
- testGumBallMachineIsBroken (){
53
- testGumBallMachine .IsBroken ();
54
- assertFalse (testGumBallMachine .IsBroken );
55
- }
62
+ }
63
+
64
+ @ Test
65
+ void testGetGumBall_InvalidCoin () {
66
+ assertThrows (
67
+ invalidCoinInsertedException .class ,
68
+ () -> {
69
+ testGumBallMachine .getGumBall (0.01 ); // Try to insert an invalid coin
70
+ });
71
+ }
72
+
73
+ @ Test
74
+ void testGumBallMachineIsBroken () {
75
+ testGumBallMachine .breakMachine ();
76
+ assertTrue (testGumBallMachine .isBroken ());
77
+ }
56
78
}
57
-
0 commit comments