Skip to content

Instantly share code, notes, and snippets.

@mauricioaniche
Created May 10, 2019 11:58
Show Gist options
  • Save mauricioaniche/2200d8c1dcab41e7c4dddcc46211f4c0 to your computer and use it in GitHub Desktop.
Save mauricioaniche/2200d8c1dcab41e7c4dddcc46211f4c0 to your computer and use it in GitHub Desktop.
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;
}
}
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);
}
}
package tudelft.sqt.mocks;
import java.util.Calendar;
public interface Clock {
Calendar today();
}
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