-
-
Save mauricioaniche/2200d8c1dcab41e7c4dddcc46211f4c0 to your computer and use it in GitHub Desktop.
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 tudelft.sqt.mocks; | |
import java.util.Calendar; | |
public class ChristmasDiscount { | |
private Clock clock; | |
public ChristmasDiscount(Clock clock) { | |
this.clock = clock; | |
} | |
public double discount(double value) { | |
Calendar today = clock.today(); | |
boolean isDecember = today.get(Calendar.MONTH) == Calendar.DECEMBER; | |
boolean isTwentyFifth = today.get(Calendar.DAY_OF_MONTH) == 25; | |
boolean isChristmas = isDecember && isTwentyFifth; | |
return isChristmas ? value * 0.10 : 0; | |
} | |
} |
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 tudelft.sqt.mocks; | |
import org.junit.jupiter.api.Assertions; | |
import org.junit.jupiter.api.Test; | |
import org.mockito.Mockito; | |
import java.util.Calendar; | |
import java.util.GregorianCalendar; | |
public class ChristmasDiscountTest { | |
@Test | |
void isChristmas() { | |
Calendar christmas = new GregorianCalendar(2019,Calendar.DECEMBER, 25); | |
Clock fakeClock = Mockito.mock(Clock.class); | |
Mockito.when(fakeClock.today()).thenReturn(christmas); | |
double discountValue = new ChristmasDiscount(fakeClock).discount(100); | |
Assertions.assertEquals(10, discountValue, 0.0001); | |
} | |
@Test | |
void notChristmas() { | |
Calendar christmas = new GregorianCalendar(2019,Calendar.DECEMBER, 26); | |
Clock fakeClock = Mockito.mock(Clock.class); | |
Mockito.when(fakeClock.today()).thenReturn(christmas); | |
double discountValue = new ChristmasDiscount(fakeClock).discount(100); | |
Assertions.assertEquals(0, discountValue, 0.0001); | |
} | |
} |
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 tudelft.sqt.mocks; | |
import java.util.Calendar; | |
public interface Clock { | |
Calendar today(); | |
} |
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 tudelft.sqt.mocks; | |
import java.util.Calendar; | |
public class DefaultClock implements Clock{ | |
@Override | |
public Calendar today() { | |
return Calendar.getInstance(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment