Created
September 11, 2020 22:17
-
-
Save JaredEzz/36bcc6d41c9753f329fd1813d3e4c262 to your computer and use it in GitHub Desktop.
Tests for Beto to not be paranoid 😳
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.jaredhasson; | |
import static org.junit.jupiter.api.Assertions.assertAll; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
import static org.junit.jupiter.api.Assertions.assertTrue; | |
import static org.junit.jupiter.api.Assertions.assertNull; | |
import static org.junit.jupiter.api.Assertions.assertNotNull; | |
import static org.junit.jupiter.api.Assertions.assertThrows; | |
import static org.junit.jupiter.api.Assumptions.assumeTrue; | |
import static org.junit.jupiter.api.Assumptions.assumingThat; | |
import org.junit.jupiter.api.*; | |
import org.junit.jupiter.params.ParameterizedTest; | |
import org.junit.jupiter.params.provider.CsvSource; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.time.DayOfWeek; | |
import java.time.LocalDateTime; | |
@DisplayName("Gumball Machine Tests") | |
public class GumballMachineTest { | |
private static Logger logger = LoggerFactory.getLogger(GumballMachineTest.class); | |
private GumballMachine classUnderTest; | |
@Test | |
@DisplayName("Before initializing, classUnderTest should be null") | |
public void nullTest() { | |
assertNull(classUnderTest); | |
} | |
@BeforeAll | |
public static void setUp() { | |
logger.info("Starting tests"); | |
} | |
@AfterAll | |
public static void tearDown() { | |
logger.info("Finished testing"); | |
} | |
@Nested | |
@DisplayName("Add Gumballs Tests") | |
public class AddGumballsTest { | |
@BeforeEach | |
public void setUp() { | |
classUnderTest = new GumballMachine(); | |
classUnderTest.setCurrentState(new EmptyState()); | |
} | |
@AfterEach | |
public void tearDown() { | |
classUnderTest = null; | |
} | |
@Test | |
@DisplayName("Adding gumballs switches to filled state") | |
public void addGumballs_emptyState_filledState() { | |
assertNotNull(classUnderTest); | |
int numOfGumballs = 5; | |
classUnderTest.addGumballs(numOfGumballs); | |
assertEquals(numOfGumballs, classUnderTest.getCount()); | |
assertTrue(classUnderTest.getCurrentState() instanceof FilledState); | |
} | |
@Test | |
@DisplayName("Adding gumballs remains in filled state") | |
public void addGumballs_filledState_filledState() { | |
assertNotNull(classUnderTest); | |
classUnderTest.setCurrentState(new FilledState()); | |
int numOfGumballs = 5; | |
classUnderTest.addGumballs(numOfGumballs); | |
assertEquals(numOfGumballs, classUnderTest.getCount()); | |
assertTrue(classUnderTest.getCurrentState() instanceof FilledState); | |
} | |
@ParameterizedTest(name = "adding {0} gumballs should result in a count of {0}") | |
@DisplayName("Parameterized test for adding gumballs") | |
@CsvSource({"3", "5", "6"}) | |
public void addGumballs_emptyState_filledState_param(int numGumballs) { | |
assertNotNull(classUnderTest); | |
classUnderTest.addGumballs(numGumballs); | |
assertEquals(numGumballs, classUnderTest.getCount()); | |
assertTrue(classUnderTest.getCurrentState() instanceof FilledState); | |
} | |
} | |
@Nested | |
@DisplayName("Add Quarters Test") | |
public class AddQuarterTests { | |
@BeforeEach | |
public void setUp() { | |
classUnderTest = new GumballMachine(); | |
classUnderTest.setCurrentState(new EmptyState()); | |
} | |
@AfterEach | |
public void tearDown() { | |
classUnderTest = null; | |
} | |
@Test | |
@Disabled | |
@DisplayName("Disabled test") | |
public void addPenny() { | |
logger.info("Should never be run"); | |
} | |
@Test | |
@DisplayName("Inserting a quarter in empty state should switch to quarter empty state") | |
public void insertQuarter_emptyState_quarterEmptyState() { | |
assertNotNull(classUnderTest); | |
classUnderTest.insertQuarter(); | |
assertTrue(classUnderTest.getCurrentState() instanceof QuarterEmptyState); | |
} | |
@Test | |
@DisplayName("Inserting a quarter in filled state should switch to filled quarter state") | |
public void insertQuarter_filledState_filledQuarterState() { | |
assertNotNull(classUnderTest); | |
classUnderTest.setCurrentState(new FilledState()); | |
classUnderTest.insertQuarter(); | |
assertTrue(classUnderTest.getCurrentState() instanceof FilledQuarterState); | |
} | |
} | |
@Nested | |
@DisplayName("Remove Quarters Test") | |
public class RemoveQuarterTests { | |
@BeforeEach | |
public void setUp() { | |
classUnderTest = new GumballMachine(); | |
classUnderTest.setCurrentState(new EmptyState()); | |
} | |
@AfterEach | |
public void tearDown() { | |
classUnderTest = null; | |
} | |
@Test | |
@DisplayName("Removing a quarter in empty state should throw exception") | |
public void removeQuarter_emptyState_exception() { | |
assertNotNull(classUnderTest); | |
assertThrows(IllegalStateException.class, () -> classUnderTest.removeQuarter()); | |
} | |
@Test | |
@DisplayName( | |
"Removing a quarter in filled quarter state on Sunday should switch to empty quarter state") | |
public void removeQuarter_filledQuarterState_emptyQuarterState() { | |
assertNotNull(classUnderTest); | |
classUnderTest.setCurrentState(new FilledQuarterState()); | |
DayOfWeek dayOfWeek = LocalDateTime.now().getDayOfWeek(); | |
assumingThat( | |
dayOfWeek == DayOfWeek.SUNDAY, | |
() -> { | |
classUnderTest.removeQuarter(); | |
assertTrue(classUnderTest.getCurrentState() instanceof FilledState); | |
}); | |
} | |
} | |
@Nested | |
@DisplayName("Turn Handle Test") | |
public class TurnHandleTests { | |
@BeforeEach | |
public void setUp() { | |
classUnderTest = new GumballMachine(); | |
classUnderTest.setCurrentState(new EmptyState()); | |
} | |
@AfterEach | |
public void tearDown() { | |
classUnderTest = null; | |
} | |
@Test | |
@Tag("conditional") | |
@DisplayName("Turn handle in the United States should switch") | |
public void turnHandle_filledQuarterState_quarterEmptyState() { | |
classUnderTest.setCurrentState(new FilledQuarterState()); | |
classUnderTest.increaseGumballCount(1); | |
assumeTrue(System.getProperty("user.country").equals("US")); | |
classUnderTest.turnHandle(); | |
assertAll( | |
"should empty the machine of gumballs and quarter", | |
() -> assertTrue(classUnderTest.getCurrentState() instanceof EmptyState), | |
() -> assertEquals(0, classUnderTest.getCount())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment