mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
Guard quality in constructor
This commit is contained in:
parent
0c8aa134b8
commit
3377402d31
@ -4,11 +4,22 @@ import com.gildedrose.Item;
|
||||
|
||||
public class BaseItem {
|
||||
private final Item item;
|
||||
private final static int MIN_QUALITY = 0;
|
||||
protected final static int MIN_QUALITY = 0;
|
||||
public BaseItem(Item item) {
|
||||
validate(item);
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
private void validate(Item item) {
|
||||
if(item.quality < MIN_QUALITY) {
|
||||
throw new IllegalArgumentException(
|
||||
"Item has a quality out of accepted boundaries. Minimum acceptable quality is "
|
||||
+ " but received "
|
||||
+ item.quality
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public int getSellIn() {
|
||||
return item.sellIn;
|
||||
}
|
||||
|
||||
@ -72,4 +72,9 @@ class BaseItemTest {
|
||||
assertEquals(0, testItem.getQuality());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAnItemWithLessThanMinimumQuality_whenCreated_thenAnExceptionIsThrown() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new BaseItem(new Item("test_item", -1, BaseItem.MIN_QUALITY - 1)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user