-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19c51b6
commit f7f7b94
Showing
13 changed files
with
305 additions
and
58 deletions.
There are no files selected for viewing
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
55 changes: 55 additions & 0 deletions
55
src/main/java/br/pro/diegoquirino/calculadora/model/CarrinhoDeCompras.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package br.pro.diegoquirino.calculadora.model; | ||
|
||
import br.pro.diegoquirino.calculadora.util.DescontoUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
|
||
public class CarrinhoDeCompras { | ||
|
||
private Map<Produto, Integer> produtos; | ||
|
||
private Cliente cliente; | ||
|
||
public CarrinhoDeCompras() { | ||
super(); | ||
this.produtos = new HashMap<>(); | ||
} | ||
|
||
public Map<Produto, Integer> getProdutos() { | ||
return produtos; | ||
} | ||
|
||
public void setProdutos(Map<Produto, Integer> produtos) { | ||
this.produtos = produtos; | ||
} | ||
|
||
public Cliente getCliente() { | ||
return cliente; | ||
} | ||
|
||
public void setCliente(Cliente cliente) { | ||
this.cliente = cliente; | ||
} | ||
|
||
public void addProduto(Produto p, int quant) { | ||
if(!this.produtos.containsKey(p)) { | ||
this.produtos.put(p, quant); | ||
} | ||
} | ||
|
||
public double calcularValorComDesconto(Produto p) { | ||
DescontoUtils du = new DescontoUtils(); | ||
double valorComDesconto = 0; | ||
for(Produto prod: this.produtos.keySet()) { | ||
if(p.equals(prod)) { | ||
valorComDesconto = prod.getValor() * | ||
du.calcularFatorDeDesconto(this.cliente.getTipoCliente(), | ||
produtos.get(prod).intValue()); | ||
} | ||
} | ||
return valorComDesconto; | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/br/pro/diegoquirino/calculadora/model/Cliente.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package br.pro.diegoquirino.calculadora.model; | ||
|
||
public interface Cliente { | ||
|
||
public String getTipoCliente(); | ||
|
||
public void setTipoCliente(String tipo); | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/br/pro/diegoquirino/calculadora/model/Produto.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package br.pro.diegoquirino.calculadora.model; | ||
|
||
public interface Produto { | ||
|
||
public double getValor(); | ||
|
||
public void setValor(double valor); | ||
|
||
public double getQuantEmEstoque(); | ||
|
||
public void setQuantEmEstoque(int quant); | ||
|
||
} |
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
45 changes: 45 additions & 0 deletions
45
src/test/java/br/pro/diegoquirino/calculadora/model/CarrinhoDeComprasTests.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package br.pro.diegoquirino.calculadora.model; | ||
|
||
import org.easymock.EasyMock; | ||
import org.easymock.EasyMockSupport; | ||
import org.easymock.Mock; | ||
import org.easymock.TestSubject; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@ExtendWith(EasyMockExtension.class) | ||
public class CarrinhoDeComprasTests extends EasyMockSupport{ | ||
|
||
// 1. Criar os Mocks | ||
@Mock | ||
private Produto produto; | ||
@Mock | ||
private Cliente cliente; | ||
|
||
@TestSubject // 2. Escolher a classe que sera testada (alvo do teste) | ||
private final CarrinhoDeCompras driver = new CarrinhoDeCompras(); | ||
|
||
@Test | ||
@DisplayName("Calcular o VALOR DO PRODUTO após aplicado o fator de desconto para o Cliente A, menor que 100 unidades") | ||
@Tag("ClienteA") | ||
public void testCalcularValorComDescontoDoProduto_QuandoClienteAMenor100() { | ||
// 3. Configurar o estado inicial dos objetos | ||
EasyMock.expect(this.cliente.getTipoCliente()).andReturn("A"); | ||
EasyMock.expect(this.produto.getValor()).andReturn(1000.00); | ||
// 4. Gravar os estados atuais dos objetos | ||
replayAll(); | ||
|
||
// 5. Testar | ||
this.driver.setCliente(this.cliente); | ||
this.driver.addProduto(this.produto, 99); | ||
double valorComDesconto = this.driver.calcularValorComDesconto(this.produto); | ||
assertEquals(900.00, valorComDesconto); | ||
// 6. Verificar que tudo o que era necess�rio para testar foi chamado | ||
verifyAll(); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/test/java/br/pro/diegoquirino/calculadora/model/EasyMockExtension.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package br.pro.diegoquirino.calculadora.model; | ||
|
||
import org.easymock.EasyMockSupport; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.TestInstancePostProcessor; | ||
|
||
public class EasyMockExtension implements TestInstancePostProcessor { | ||
|
||
@Override | ||
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception { | ||
EasyMockSupport.injectMocks(testInstance); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/test/java/br/pro/diegoquirino/calculadora/util/ClienteAStub.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package br.pro.diegoquirino.calculadora.util; | ||
|
||
import br.pro.diegoquirino.calculadora.model.Cliente; | ||
|
||
public class ClienteAStub implements Cliente { | ||
|
||
@Override | ||
public String getTipoCliente() { | ||
return "A"; | ||
} | ||
|
||
@Override | ||
public void setTipoCliente(String tipo) { | ||
; | ||
} | ||
|
||
} |
45 changes: 0 additions & 45 deletions
45
src/test/java/br/pro/diegoquirino/calculadora/util/DescontoUtilsTest.java
This file was deleted.
Oops, something went wrong.
97 changes: 97 additions & 0 deletions
97
src/test/java/br/pro/diegoquirino/calculadora/util/DescontoUtilsTests.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package br.pro.diegoquirino.calculadora.util; | ||
|
||
import br.pro.diegoquirino.calculadora.model.Cliente; | ||
import org.junit.jupiter.api.*; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class DescontoUtilsTests { | ||
|
||
private DescontoUtils driver; | ||
private Cliente cli; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.driver = new DescontoUtils(); | ||
this.cli = new ClienteAStub(); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
this.driver = null; | ||
this.cli = null; | ||
} | ||
|
||
@Test | ||
@DisplayName("Calcular o fator de desconto para o Cliente A, menor que 100 unidades") | ||
@Tag("ClienteA") | ||
void testCalcularFatorDeDescontoClienteAMenor100() { | ||
double fator = this.driver.calcularFatorDeDesconto(this.cli.getTipoCliente(),99); | ||
assertEquals(0.90, fator); | ||
} | ||
|
||
@Test | ||
@DisplayName("Calcular o fator de desconto para o Cliente A, maior ou igual a 100 e menor que 1000 unidades") | ||
@Tag("ClienteA") | ||
void testCalcularFatorDeDescontoClienteAEntre100e1000() { | ||
double fator = this.driver.calcularFatorDeDesconto(this.cli.getTipoCliente(),100); | ||
assertEquals(0.95, fator); | ||
} | ||
|
||
@Test | ||
@DisplayName("Calcular o fator de desconto para o Cliente A, maior ou igual 1000 unidades") | ||
@Tag("ClienteA") | ||
void testCalcularFatorDeDescontoClienteAMaior1000() { | ||
double fator = this.driver.calcularFatorDeDesconto(this.cli.getTipoCliente(),1000); | ||
assertEquals(1.00, fator); | ||
} | ||
|
||
@Test | ||
@DisplayName("Calcular o fator de desconto para o Cliente B, menor que 100 unidades") | ||
@Tag("ClienteB") | ||
void testCalcularFatorDeDescontoClienteBMenor100() { | ||
double fator = this.driver.calcularFatorDeDesconto("B",99); | ||
assertEquals(0.85, fator); | ||
} | ||
|
||
@Test | ||
@DisplayName("Calcular o fator de desconto para o Cliente B, maior ou igual a 100 e menor que 1000 unidades") | ||
@Tag("ClienteB") | ||
void testCalcularFatorDeDescontoClienteBEntre100e1000() { | ||
double fator = this.driver.calcularFatorDeDesconto("B",100); | ||
assertEquals(0.90, fator); | ||
} | ||
|
||
@Test | ||
@DisplayName("Calcular o fator de desconto para o Cliente B, maior ou igual 1000 unidades") | ||
@Tag("ClienteB") | ||
void testCalcularFatorDeDescontoClienteBMaior1000() { | ||
double fator = this.driver.calcularFatorDeDesconto("B",1000); | ||
assertEquals(0.95, fator); | ||
} | ||
|
||
@Test | ||
@DisplayName("Calcular o fator de desconto para o Cliente C, menor que 100 unidades") | ||
@Tag("ClienteC") | ||
void testCalcularFatorDeDescontoClienteCMenor100() { | ||
double fator = this.driver.calcularFatorDeDesconto("C",99); | ||
assertEquals(0.80, fator); | ||
} | ||
|
||
@Test | ||
@DisplayName("Calcular o fator de desconto para o Cliente C, maior ou igual a 100 e menor que 1000 unidades") | ||
@Tag("ClienteC") | ||
void testCalcularFatorDeDescontoClienteCEntre100e1000() { | ||
double fator = this.driver.calcularFatorDeDesconto("C",100); | ||
assertEquals(0.85, fator); | ||
} | ||
|
||
@Test | ||
@DisplayName("Calcular o fator de desconto para o Cliente C, maior ou igual 1000 unidades") | ||
@Tag("ClienteC") | ||
void testCalcularFatorDeDescontoClienteCMaior1000() { | ||
double fator = this.driver.calcularFatorDeDesconto("C",1000); | ||
assertEquals(0.90, fator); | ||
} | ||
|
||
} |
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
Oops, something went wrong.