11package com .codedifferently .lesson16 ;
22
33import static org .junit .jupiter .api .Assertions .assertEquals ;
4- import static org .junit .jupiter .api .Assertions .assertFalse ;
54import static org .junit .jupiter .api .Assertions .assertNotEquals ;
65import 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 ;
710import org .junit .jupiter .api .Test ;
811
912public 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+ }
1119
1220 @ 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+ }
2126
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
2933
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 ++;
3239 }
33- @ Test
34- testGumballMachineIsEmpty_whenEmpty () {
35- // Arrange
36-
3740
38- // Act
39- testGumballMachine .getGumBall (threedollars );
41+ assertNotEquals (
42+ previousColor , newColor , "The color should have changed after multiple attempts" );
43+ }
4044
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
4356 }
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
4961 });
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+ }
5678}
57-
0 commit comments