Last active
January 23, 2017 20:06
-
-
Save alexandreaquiles/9956165 to your computer and use it in GitHub Desktop.
Exemplo de como a classe Clock da nova API de datas do Java 8 permite uma maior testabilidade. Faz parte do post: http://blog.caelum.com.br/conheca-a-nova-api-de-datas-do-java-8/
This file contains hidden or 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
| import java.time.LocalDate; | |
| import java.time.temporal.TemporalAdjusters; | |
| public class DatasImportantesNaoTestavel { | |
| public LocalDate diaDoProgramadorDesseAno(){ | |
| //como fazer para testar uma data do passado, por exemplo, de 2012? | |
| return LocalDate.now().with(TemporalAdjusters.firstDayOfYear()).plusDays(255); | |
| } | |
| } |
This file contains hidden or 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
| import java.time.Clock; | |
| import java.time.LocalDate; | |
| import java.time.temporal.TemporalAdjusters; | |
| public class DatasImportantesTestavel { | |
| private Clock clock; | |
| public DatasImportantesTestavel(Clock clock) { | |
| this.clock = clock; | |
| } | |
| public LocalDate diaDoProgramadorDesseAno(){ | |
| //passando o Clock, uma dependência injetada pelo construtor, | |
| //para a factory de LocalDate, podemos apontar para uma data do passado | |
| return LocalDate.now(clock).with(TemporalAdjusters.firstDayOfNextYear()).plusDays(255); | |
| } | |
| } |
This file contains hidden or 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
| import java.time.Clock; | |
| import java.time.Instant; | |
| import java.time.LocalDate; | |
| import java.time.Month; | |
| import java.time.MonthDay; | |
| import java.time.ZoneId; | |
| import org.junit.Assert; | |
| import org.junit.Test; | |
| public class DiaDoProgramadorTest { | |
| private final ZoneId AMERICA_SAO_PAULO = ZoneId.of("America/Sao_Paulo"); | |
| private final MonthDay QUINZE_DE_ABRIL = MonthDay.of(Month.APRIL, 15); | |
| @Test | |
| public void diaDoProgramadorDe2014DeveSerEm13DeSetembro() { | |
| Instant instanteEm2014 = QUINZE_DE_ABRIL.atYear(2014).atStartOfDay().atZone(AMERICA_SAO_PAULO).toInstant(); | |
| Clock clock = Clock.fixed(instanteEm2014, AMERICA_SAO_PAULO); //Clock fixado em 2014 | |
| DatasImportantesTestavel datasImportantes = new DatasImportantesTestavel(clock); | |
| LocalDate diaDoProgramadorDe2014 = datasImportantes.diaDoProgramadorDesseAno(); | |
| Assert.assertEquals(MonthDay.of(Month.SEPTEMBER, 13).atYear(2014), diaDoProgramadorDe2014); | |
| } | |
| @Test | |
| public void diaDoProgramadorDe2012DeveSerEm12DeSetembro() { | |
| Instant instanteEm2012 = QUINZE_DE_ABRIL.atYear(2012).atStartOfDay().atZone(AMERICA_SAO_PAULO).toInstant(); | |
| Clock clock = Clock.fixed(instanteEm2012, AMERICA_SAO_PAULO); //Clock fixado em 2012 | |
| DatasImportantesTestavel datasImportantes = new DatasImportantesTestavel(clock); | |
| LocalDate diaDoProgramadorDe2012 = datasImportantes.diaDoProgramadorDesseAno(); | |
| Assert.assertEquals(MonthDay.of(Month.SEPTEMBER, 12).atYear(2012), diaDoProgramadorDe2012); | |
| } | |
| } |
This file contains hidden or 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
| import java.time.Clock; | |
| import java.time.LocalDate; | |
| public class ProgramaPrincipal { | |
| public static void main(String[] args) { | |
| Clock clock = Clock.systemDefaultZone(); //Clock com o timezone padrão do sistema operacional | |
| DatasImportantesTestavel datasImportantes = new DatasImportantesTestavel(clock); | |
| LocalDate diaDoProgramadorDesseAno = datasImportantes.diaDoProgramadorDesseAno(); | |
| System.out.println(diaDoProgramadorDesseAno); //algo como 2014-09-13 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment