extend unit tests

This commit is contained in:
Velizar Todorov 2021-11-26 14:17:23 +01:00
parent b28c8023e0
commit 8807cd17ce
7 changed files with 199 additions and 15 deletions

View File

@ -13,26 +13,25 @@ import static com.gildedrose.items.TestHelper.testItemException;
@TestMethodOrder(OrderAnnotation.class)
class AgedBrieItemTest {
private static final Item AGED_ITEM_INCREMENT_BY_ONE = new Item(AGED_BRIE.toString(), 5, 20);
private static final Item AGED_ITEM_INCREMENT_QUALITY_BY_TWO = new Item(AGED_BRIE.toString(), 2, 4);
private static final Item AGED_ITEM_ERROR = new Item(AGED_BRIE.toString(), 10, -5);
private final Item item = new Item(AGED_BRIE.toString(), 5, 20);
private final Item itemError = new Item(AGED_BRIE.toString(), 10, -5);
@Test
@Order(1)
void incrementQualityByOneSuccess() {
testItem(AGED_ITEM_INCREMENT_BY_ONE, 2, 22, 3);
testItem(item, 2, 3, 22);
}
@Test
@Order(2)
void incrementQualityByTwoSuccess() {
testItem(AGED_ITEM_INCREMENT_QUALITY_BY_TWO, 3, 8, -1);
testItem(item, 7, -2, 29);
}
@Test
@Order(3)
void negativeQualityFail() {
testItemException(AGED_ITEM_ERROR);
testItemException(itemError);
}
}

View File

@ -0,0 +1,49 @@
package com.gildedrose.items;
import com.gildedrose.main.Item;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import static com.gildedrose.item_helpers.ItemName.BACKSTAGE_PASS;
import static com.gildedrose.items.TestHelper.testItem;
import static com.gildedrose.items.TestHelper.testItemException;
@TestMethodOrder(OrderAnnotation.class)
class BackstagePassItemTest {
private final Item item = new Item(BACKSTAGE_PASS.toString(), 15, 20);
private final Item itemError = new Item(BACKSTAGE_PASS.toString(), 10, -5);
@Test
@Order(1)
void testIncrementQualityByOneSuccess() {
testItem(item, 5, 10, 25);
}
@Test
@Order(2)
void testIncrementQualityByTwoSuccess() {
testItem(item, 10, 5, 35);
}
@Test
@Order(3)
void testIncrementQualityByThreeSuccess() {
testItem(item, 15, 0, 50);
}
@Test
@Order(4)
void testQualityZeroSuccess() {
testItem(item, 16, -1, 0);
}
@Test
@Order(2)
void negativeQualityFail() {
testItemException(itemError);
}
}

View File

@ -0,0 +1,36 @@
package com.gildedrose.items;
import com.gildedrose.main.Item;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import static com.gildedrose.item_helpers.ItemName.CONJURED;
import static com.gildedrose.items.TestHelper.testItem;
import static com.gildedrose.items.TestHelper.testItemException;
@TestMethodOrder(OrderAnnotation.class)
class ConjuredItemTest {
private final Item item = new Item(CONJURED.toString(), 5, 20);
private final Item itemError = new Item(CONJURED.toString(), 10, -5);
@Test
@Order(1)
void decrementQualityByOneSuccess() {
testItem(item, 2, 3, 18);
}
@Test
@Order(2)
void decrementQualityByFourSuccess() {
testItem(item, 10, -5, 0);
}
@Test
@Order(3)
void negativeQualityFail() {
testItemException(itemError);
}
}

View File

@ -0,0 +1,52 @@
package com.gildedrose.items;
import com.gildedrose.main.GildedRose;
import com.gildedrose.main.Item;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import static com.gildedrose.item_helpers.ItemName.LEGENDARY;
import static com.gildedrose.items.LegendaryItem.LEGENDARY_ITEM_QUALITY;
import static com.gildedrose.items.LegendaryItem.NOT_LEGENDARY_ITEM_ERROR_MESSAGE;
import static com.gildedrose.items.TestHelper.testItem;
import static com.gildedrose.items.TestHelper.testItemException;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@TestMethodOrder(OrderAnnotation.class)
class LegendaryItemTest {
private final Item item = new Item(LEGENDARY.toString(), 5, 80);
private final Item fakeLegendaryItem = new Item(LEGENDARY.toString(), 5, 75);
private final Item itemError = new Item(LEGENDARY.toString(), 10, -5);
@Test
@Order(1)
void getLegendaryQualityBeforeSellInDateSuccess() {
testItem(item, 2, 3, LEGENDARY_ITEM_QUALITY);
}
@Test
@Order(2)
void getLegendaryQualityPassSellInDateSuccess() {
testItem(item, 10, -5, LEGENDARY_ITEM_QUALITY);
}
@Test
@Order(3)
void testFakeLegendaryItemExceptionFail() {
GildedRose gildedRose = new GildedRose(fakeLegendaryItem);
Exception exception = assertThrows(IllegalArgumentException.class, gildedRose::updateQuality);
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(NOT_LEGENDARY_ITEM_ERROR_MESSAGE));
}
@Test
@Order(4)
void negativeQualityFail() {
testItemException(itemError);
}
}

View File

@ -0,0 +1,47 @@
package com.gildedrose.items;
import com.gildedrose.main.GildedRose;
import com.gildedrose.main.Item;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.util.List;
import static java.util.Arrays.asList;
@TestMethodOrder(OrderAnnotation.class)
class MultipleItemsTest {
protected static final String BACKSTAGE_PASS = "Backstage passes to a TAFKAL80ETC concert";
protected static final String LEGENDARY_ITEM_NAME = "Sulfuras, Hand of Ragnaros";
private final Item normalItem1 = new Item("+5 Dexterity Vest", 10, 20);
private final Item agedBrieItem = new Item("Aged Brie", 2, 0);
private final Item normalItem2 = new Item("Elixir of the Mongoose", 5, 7);
private final Item legendaryItem1 = new Item(LEGENDARY_ITEM_NAME, 0, 80);
private final Item legendaryItem2 = new Item(LEGENDARY_ITEM_NAME, -1, 80);
private final Item backStageItem1 = new Item(BACKSTAGE_PASS, 15, 20);
private final Item backStageItem2 = new Item(BACKSTAGE_PASS, 10, 49);
private final Item backStageItem3 = new Item(BACKSTAGE_PASS, 5, 49);
private final Item conjuredItem = new Item("Conjured Mana Cake", 3, 6);
public List<Item> getOriginalTestItems() {
return asList(normalItem1, agedBrieItem, normalItem2, legendaryItem1, legendaryItem2,
backStageItem1, backStageItem2, backStageItem3, conjuredItem);
}
@Test
void test() {
List<Item> items = getOriginalTestItems();
GildedRose gildedRose = new GildedRose(items);
int days = 20;
for (int i = 0; i < days; i++) {
for (Item item : items) {
System.out.println(item);
}
gildedRose.updateQuality();
}
}
}

View File

@ -13,25 +13,24 @@ import static com.gildedrose.items.TestHelper.testItemException;
@TestMethodOrder(OrderAnnotation.class)
class NormalItemTest {
private static final Item NORMAL_ITEM = new Item(NORMAL.toString(), 5, 20);
private static final Item NORMAL_ITEM_INCREMENT_BY_TWO = new Item(NORMAL.toString(), 10, 20);
private static final Item NORMAL_ITEM_ERROR = new Item(NORMAL.toString(), 10, -5);
private final Item item = new Item(NORMAL.toString(), 5, 20);
private final Item itemError = new Item(NORMAL.toString(), 10, -5);
@Test
@Order(1)
void decrementQualityByOneSuccess() {
testItem(NORMAL_ITEM, 2, 18, 3);
testItem(item, 2, 3, 18);
}
@Test
@Order(2)
void decrementQualityByTwoSuccess() {
testItem(NORMAL_ITEM_INCREMENT_BY_TWO, 15, 0, -5);
testItem(item, 10, -5, 5);
}
@Test
@Order(3)
void negativeQualityFail() {
testItemException(NORMAL_ITEM_ERROR);
testItemException(itemError);
}
}

View File

@ -8,16 +8,17 @@ import static org.junit.jupiter.api.Assertions.*;
public class TestHelper {
public static void testItem(Item item, int days, int expectedQuality, int expectedSellIn) {
public static void testItem(Item item, int daysToPass, int expectedSellIn, int expectedQuality) {
// given
GildedRose app = new GildedRose(item);
// when
for (int i = 0; i < days; i++) {
for (int i = 0; i < daysToPass; i++) {
app.updateQuality();
System.out.println(item);
}
//then
assertEquals(expectedQuality, item.quality);
assertEquals(expectedSellIn, item.sellIn);
assertEquals(expectedQuality, item.quality);
}
public static void testItemException(Item item) {
@ -26,4 +27,5 @@ public class TestHelper {
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(QUALITY_ERROR_MESSAGE));
}
}