Testes que lidam demais com outros objetos ao invés de lidar com o objeto sob teste podem estar avisando o desenvolvedor em relação a problemas de encapsulamento. A própria não utilização da Lei de Demeter, tanto nos testes quanto no código de produção, também pode avisar sobre os mesmos problemas.
Created
January 31, 2017 13:24
-
-
Save joffilyfe/b809498300b68acf4abf3677168a2391 to your computer and use it in GitHub Desktop.
TDD E Encapsulamento
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
public class TestaFatura { | |
@Test | |
public void deveMarcarFaturaComoPagaCasoBoletoUnicoPagueTudo() { | |
ProcessadorDeBoletos processador = new ProcessadorDeBoletos(); | |
Fatura fatura = new Fatura("Cliente", 150.0); | |
Boleto b1 = new Boleto(150.0); | |
List<Boleto> boletos = Arrays.asList(b1); | |
processador.processa(boletos, fatura); | |
// Onde a fatura acaba sabendo que foi paga? | |
assertTrue(fatura.isPago()); | |
} | |
} |
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
public class ProcessaBoleto { | |
public void processa(List<Boleto> boletos, Fatura fatura) { | |
double valorTotal = 0; | |
for(Boleto boleto : boletos) { | |
Pagamento pagamento = new Pagamento(boleto.getValor(), MeioDePagamento.BOLETO); | |
fatura.getPagamentos().add(pagamento); | |
valorTotal += boleto.getValor(); | |
} | |
if(valorTotal >= fatura.getValor()) { | |
fatura.setPago(true); | |
} | |
} | |
} |
Nós acabamos de perceber que o processador de boletos sabe demais sobre a classe de Fatura. Quando mandamos processar o boleto a nós checamos se o valor acumulado é maior ou igual ao da fatura e a marcamos como paga.
E se nós encapsularmos esse comportamento dentro da propria fatura? Evitariamos que o gerador de boleto soubesse demais sobre a fatura e o seu código se tornaria mais enxuto.
Primeiro vamos adicionar o método addPagamento(Pagamento p) a fatura e posteriormente remover o if de dentro do gerenciador de boleto.
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
public class Fatura { | |
private List<Pagamento> pagamentos; | |
private boolean paga; | |
private double valor; | |
public Fatura(double valor) { | |
this.pagamentos = new ArrayList<Pagamento>(); | |
this.paga = false; | |
this.valor = valor; | |
} | |
/* | |
* Removemos a lógica do gerenciador de boletos para a classe interessada | |
*/ | |
public addPagamento(Pagamento p) { | |
this.pagamentos.add(p); | |
double valorTotla = 0; | |
for (Pagamento pagamento : this.pagamentos) { | |
valorTotal += pagamento.getValor(); | |
} | |
if (valorTotal >= this.valor) { | |
this.paga = true; | |
} | |
} | |
} |
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
public class ProcessaBoleto { | |
public void processa(List<Boleto> boletos, Fatura fatura) { | |
for(Boleto boleto : boletos) { | |
Pagamento pagamento = new Pagamento(boleto.getValor(), MeioDePagamento.BOLETO); | |
fatura.addPagamento(pagamento); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment