public class PriceCalculator {
private int quantity;
private double unitPrice;
private double discount;
public PriceCalculator(int quantity, double price, double discount){
this.quantity = quantity;
this.unitPrice = price;
this.discount = discount;
}
public double getPrice(){
return this.quantity * this.unitPrice * ( this.discount / 100 );
}
}
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class RunWithOneTestCase {
private final static int QUANTITY1 = 5;
private final static double PRICE1 = 10;
private final static double DISCOUNT1 = 90;
private final static double EXPECTED1 = QUANTITY1 * PRICE1 * ( DISCOUNT1 / 100 );
private final static int QUANTITY2 = 4;
private final static double PRICE2 = 5;
private final static double DISCOUNT2 = 80;
private final static double EXPECTED2 = QUANTITY2 * PRICE2 * ( DISCOUNT2 / 100 );
@Test
public void testPriceCalculation(){
PriceCalculator priceCalculator = new PriceCalculator(QUANTITY1, PRICE1, DISCOUNT1);
assertEquals("price calculated for test data1",
EXPECTED1, priceCalculator.getPrice(), 0);
PriceCalculator priceCalculator2 = new PriceCalculator(QUANTITY2, PRICE2, DISCOUNT2);
assertEquals("price calculated for test data2",
EXPECTED2, priceCalculator2.getPrice(), 0);
}
}
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class RunWithTwoTestCase {
private int quantity;
private double price;
private double discount;
private double expected;
private PriceCalculator priceCalculator;
public RunWithTwoTestCase(int qty, double price, double discount, double expected){
this.quantity = qty;
this.price = price;
this.discount = discount;
this.expected = expected;
}
@Parameters(name = "{index}: (Quantity {0} * Price {1}) * (Discount{2}/100) = {3}")
public static Collection<Object[]> generateData()
{
return Arrays.asList(new Object[][] {
{ 5, 10, 90, 45 },
{ 4, 5, 80, 16 } });
}
@Before
public void setUp() throws Exception {
this.priceCalculator = new PriceCalculator(this.quantity, this.price, this.discount);
}
@Test
public void testPrice(){
assertEquals("price calculated for test data", this.expected,
this.priceCalculator.getPrice(), 0);
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
41 | |
25 | |
17 | |
14 | |
9 | |
7 | |
6 | |
6 | |
6 | |
6 |