mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
✅ extend unit tests
add AgedBrie unit test and refactor rest
This commit is contained in:
parent
143a45192c
commit
b28c8023e0
@ -8,7 +8,7 @@ import static java.lang.Math.max;
|
||||
|
||||
public class ItemHandler {
|
||||
|
||||
protected static final String QUALITY_ERROR_MESSAGE = "Quality cannot be negative! Current value: ";
|
||||
public static final String QUALITY_ERROR_MESSAGE = "Quality cannot be negative! Current value: ";
|
||||
|
||||
private final Item item;
|
||||
|
||||
|
||||
@ -36,7 +36,6 @@ class GildedRoseTest {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testLegendaryItem() {
|
||||
int days = 20;
|
||||
|
||||
@ -1,50 +0,0 @@
|
||||
package com.gildedrose;
|
||||
|
||||
import com.gildedrose.main.GildedRose;
|
||||
import com.gildedrose.main.Item;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static com.gildedrose.item_helpers.ItemName.NORMAL;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class NormalItemTest {
|
||||
|
||||
@Test
|
||||
void decrementQualityByOneSuccess() {
|
||||
// given
|
||||
Item normalItem = new Item(NORMAL.toString(), 5, 20);
|
||||
GildedRose app = new GildedRose(normalItem);
|
||||
// when
|
||||
int days = 2;
|
||||
for (int i = 0; i < days; i++) {
|
||||
app.updateQuality();
|
||||
}
|
||||
//then
|
||||
assertEquals(18, normalItem.quality);
|
||||
assertEquals(3, normalItem.sellIn);
|
||||
}
|
||||
|
||||
@Test
|
||||
void decrementQualityByTwoSuccess() {
|
||||
// given
|
||||
Item normalItem = new Item(NORMAL.toString(), 10, 20);
|
||||
GildedRose app = new GildedRose(normalItem);
|
||||
// when
|
||||
int days = 15;
|
||||
for (int i = 0; i < days; i++) {
|
||||
app.updateQuality();
|
||||
}
|
||||
//then
|
||||
assertEquals(0, normalItem.quality);
|
||||
assertEquals(-5, normalItem.sellIn);
|
||||
}
|
||||
|
||||
@Test
|
||||
void negativeQualityFail() {
|
||||
Item normalItem = new Item(NORMAL.toString(), 10, -5);
|
||||
GildedRose app = new GildedRose(normalItem);
|
||||
Exception exception = assertThrows(IllegalArgumentException.class, app::updateQuality);
|
||||
String actualMessage = exception.getMessage();
|
||||
assertTrue(actualMessage.contains("Quality cannot be negative! Current value:"));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
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.AGED_BRIE;
|
||||
import static com.gildedrose.items.TestHelper.testItem;
|
||||
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);
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void incrementQualityByOneSuccess() {
|
||||
testItem(AGED_ITEM_INCREMENT_BY_ONE, 2, 22, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void incrementQualityByTwoSuccess() {
|
||||
testItem(AGED_ITEM_INCREMENT_QUALITY_BY_TWO, 3, 8, -1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void negativeQualityFail() {
|
||||
testItemException(AGED_ITEM_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
37
Java/src/test/java/com/gildedrose/items/NormalItemTest.java
Normal file
37
Java/src/test/java/com/gildedrose/items/NormalItemTest.java
Normal file
@ -0,0 +1,37 @@
|
||||
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.NORMAL;
|
||||
import static com.gildedrose.items.TestHelper.testItem;
|
||||
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);
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void decrementQualityByOneSuccess() {
|
||||
testItem(NORMAL_ITEM, 2, 18, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void decrementQualityByTwoSuccess() {
|
||||
testItem(NORMAL_ITEM_INCREMENT_BY_TWO, 15, 0, -5);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void negativeQualityFail() {
|
||||
testItemException(NORMAL_ITEM_ERROR);
|
||||
}
|
||||
}
|
||||
29
Java/src/test/java/com/gildedrose/items/TestHelper.java
Normal file
29
Java/src/test/java/com/gildedrose/items/TestHelper.java
Normal file
@ -0,0 +1,29 @@
|
||||
package com.gildedrose.items;
|
||||
|
||||
import com.gildedrose.main.GildedRose;
|
||||
import com.gildedrose.main.Item;
|
||||
|
||||
import static com.gildedrose.item_helpers.ItemHandler.QUALITY_ERROR_MESSAGE;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestHelper {
|
||||
|
||||
public static void testItem(Item item, int days, int expectedQuality, int expectedSellIn) {
|
||||
// given
|
||||
GildedRose app = new GildedRose(item);
|
||||
// when
|
||||
for (int i = 0; i < days; i++) {
|
||||
app.updateQuality();
|
||||
}
|
||||
//then
|
||||
assertEquals(expectedQuality, item.quality);
|
||||
assertEquals(expectedSellIn, item.sellIn);
|
||||
}
|
||||
|
||||
public static void testItemException(Item item) {
|
||||
GildedRose gildedRose = new GildedRose(item);
|
||||
Exception exception = assertThrows(IllegalArgumentException.class, gildedRose::updateQuality);
|
||||
String actualMessage = exception.getMessage();
|
||||
assertTrue(actualMessage.contains(QUALITY_ERROR_MESSAGE));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user