Skip to content

Commit c9f6e92

Browse files
authored
Merge pull request #1 from fracz/specs
Specifications for today's lab
2 parents e03c145 + 2e94444 commit c9f6e92

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package pl.edu.agh.mwo.invoice;
2+
3+
import java.math.BigDecimal;
4+
5+
import org.hamcrest.Matchers;
6+
import org.junit.Assert;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import pl.edu.agh.mwo.invoice.Invoice;
11+
import pl.edu.agh.mwo.invoice.product.DairyProduct;
12+
import pl.edu.agh.mwo.invoice.product.OtherProduct;
13+
import pl.edu.agh.mwo.invoice.product.Product;
14+
import pl.edu.agh.mwo.invoice.product.TaxFreeProduct;
15+
16+
public class InvoiceTest {
17+
private Invoice invoice;
18+
19+
@Before
20+
public void createEmptyInvoiceForTheTest() {
21+
invoice = new Invoice();
22+
}
23+
24+
@Test
25+
public void testEmptyInvoiceHasEmptySubtotal() {
26+
Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getSubtotal()));
27+
}
28+
29+
@Test
30+
public void testEmptyInvoiceHasEmptyTaxAmount() {
31+
Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTax()));
32+
}
33+
34+
@Test
35+
public void testEmptyInvoiceHasEmptyTotal() {
36+
Assert.assertThat(BigDecimal.ZERO, Matchers.comparesEqualTo(invoice.getTotal()));
37+
}
38+
39+
@Test
40+
public void testInvoiceHasTheSameSubtotalAndTotalIfTaxIsZero() {
41+
Product taxFreeProduct = new TaxFreeProduct("Warzywa", new BigDecimal("199.99"));
42+
invoice.addProduct(taxFreeProduct);
43+
Assert.assertThat(invoice.getTotal(), Matchers.comparesEqualTo(invoice.getSubtotal()));
44+
}
45+
46+
@Test
47+
public void testInvoiceHasProperSubtotalForManyProducts() {
48+
invoice.addProduct(new TaxFreeProduct("Owoce", new BigDecimal("200")));
49+
invoice.addProduct(new DairyProduct("Maslanka", new BigDecimal("100")));
50+
invoice.addProduct(new OtherProduct("Wino", new BigDecimal("10")));
51+
Assert.assertThat(new BigDecimal("310"), Matchers.comparesEqualTo(invoice.getSubtotal()));
52+
}
53+
54+
@Test
55+
public void testInvoiceHasProperTaxValueForManyProduct() {
56+
// tax: 0
57+
invoice.addProduct(new TaxFreeProduct("Pampersy", new BigDecimal("200")));
58+
// tax: 8
59+
invoice.addProduct(new DairyProduct("Kefir", new BigDecimal("100")));
60+
// tax: 2.30
61+
invoice.addProduct(new OtherProduct("Piwko", new BigDecimal("10")));
62+
Assert.assertThat(new BigDecimal("10.30"), Matchers.comparesEqualTo(invoice.getTax()));
63+
}
64+
65+
@Test
66+
public void testInvoiceHasProperTotalValueForManyProduct() {
67+
// price with tax: 200
68+
invoice.addProduct(new TaxFreeProduct("Maskotki", new BigDecimal("200")));
69+
// price with tax: 108
70+
invoice.addProduct(new DairyProduct("Maslo", new BigDecimal("100")));
71+
// price with tax: 12.30
72+
invoice.addProduct(new OtherProduct("Chipsy", new BigDecimal("10")));
73+
Assert.assertThat(new BigDecimal("320.30"), Matchers.comparesEqualTo(invoice.getTotal()));
74+
}
75+
76+
@Test
77+
public void testInvoiceHasPropoerSubtotalWithQuantityMoreThanOne() {
78+
// 2x kubek - price: 10
79+
invoice.addProduct(new TaxFreeProduct("Kubek", new BigDecimal("5")), 2);
80+
// 3x kozi serek - price: 30
81+
invoice.addProduct(new DairyProduct("Kozi Serek", new BigDecimal("10")), 3);
82+
// 1000x pinezka - price: 10
83+
invoice.addProduct(new OtherProduct("Pinezka", new BigDecimal("0.01")), 1000);
84+
Assert.assertThat(new BigDecimal("50"), Matchers.comparesEqualTo(invoice.getSubtotal()));
85+
}
86+
87+
@Test
88+
public void testInvoiceHasPropoerTotalWithQuantityMoreThanOne() {
89+
// 2x chleb - price with tax: 10
90+
invoice.addProduct(new TaxFreeProduct("Chleb", new BigDecimal("5")), 2);
91+
// 3x chedar - price with tax: 32.40
92+
invoice.addProduct(new DairyProduct("Chedar", new BigDecimal("10")), 3);
93+
// 1000x pinezka - price with tax: 12.30
94+
invoice.addProduct(new OtherProduct("Pinezka", new BigDecimal("0.01")), 1000);
95+
Assert.assertThat(new BigDecimal("54.70"), Matchers.comparesEqualTo(invoice.getTotal()));
96+
}
97+
98+
@Test(expected = IllegalArgumentException.class)
99+
public void testInvoiceWithZeroQuantity() {
100+
invoice.addProduct(new TaxFreeProduct("Tablet", new BigDecimal("1678")), 0);
101+
}
102+
103+
@Test(expected = IllegalArgumentException.class)
104+
public void testInvoiceWithNegativeQuantity() {
105+
invoice.addProduct(new DairyProduct("Zsiadle mleko", new BigDecimal("5.55")), -1);
106+
}
107+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package pl.edu.agh.mwo.invoice.product;
2+
3+
import java.math.BigDecimal;
4+
5+
import org.hamcrest.Matchers;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import pl.edu.agh.mwo.invoice.product.Product;
10+
11+
public class ProductTest {
12+
@Test
13+
public void testProductNameIsCorrect() {
14+
Product product = new OtherProduct("buty", new BigDecimal("100.0"));
15+
Assert.assertEquals("buty", product.getName());
16+
}
17+
18+
@Test
19+
public void testProductPriceAndTaxWithDefaultTax() {
20+
Product product = new OtherProduct("Ogorki", new BigDecimal("100.0"));
21+
Assert.assertThat(new BigDecimal("100"), Matchers.comparesEqualTo(product.getPrice()));
22+
Assert.assertThat(new BigDecimal("0.23"), Matchers.comparesEqualTo(product.getTaxPercent()));
23+
}
24+
25+
@Test
26+
public void testProductPriceAndTaxWithDairyProduct() {
27+
Product product = new DairyProduct("Szarlotka", new BigDecimal("100.0"));
28+
Assert.assertThat(new BigDecimal("100"), Matchers.comparesEqualTo(product.getPrice()));
29+
Assert.assertThat(new BigDecimal("0.08"), Matchers.comparesEqualTo(product.getTaxPercent()));
30+
}
31+
32+
@Test
33+
public void testPriceWithTax() {
34+
Product product = new DairyProduct("Oscypek", new BigDecimal("100.0"));
35+
Assert.assertThat(new BigDecimal("108"), Matchers.comparesEqualTo(product.getPriceWithTax()));
36+
}
37+
38+
@Test(expected = IllegalArgumentException.class)
39+
public void testProductWithNullName() {
40+
new OtherProduct(null, new BigDecimal("100.0"));
41+
}
42+
43+
@Test(expected = IllegalArgumentException.class)
44+
public void testProductWithEmptyName() {
45+
new TaxFreeProduct("", new BigDecimal("100.0"));
46+
}
47+
48+
@Test(expected = IllegalArgumentException.class)
49+
public void testProductWithNullPrice() {
50+
new DairyProduct("Banany", null);
51+
}
52+
53+
@Test(expected = IllegalArgumentException.class)
54+
public void testProductWithNegativePrice() {
55+
new TaxFreeProduct("Mandarynki", new BigDecimal("-1.00"));
56+
}
57+
}

0 commit comments

Comments
 (0)