InventoryTest.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import static org.junit.Assert.*;
  2. import java.util.Arrays;
  3. import org.junit.Test;
  4. import org.junit.Before;
  5. public class InventoryTest {
  6. private static final int MAX_QUALITY = 50;
  7. private static final String AGED_BRIE_NAME = "Aged Brie";
  8. private static final String BACKSTAGE_PASS_NAME = "Backstage passes to a TAFKAL80ETC concert";
  9. private static final String NORMAL_ITEM_NAME = "+5 Dexterity Vest";
  10. private Item sulfuras;
  11. private Item normalItem;
  12. @Before
  13. public void setup(){
  14. sulfuras = new Item("Sulfuras, Hand of Ragnaros", 0, 80);
  15. normalItem = new Item(NORMAL_ITEM_NAME, 10, 20);
  16. }
  17. @Test
  18. public void testUpdate_ForSulfurasQuality() throws Exception {
  19. int expectedQuality = sulfuras.getQuality();
  20. Item[] items = {sulfuras};
  21. Inventory sut = new Inventory(items);
  22. sut.updateQuality();
  23. assertEquals(expectedQuality, sulfuras.getQuality());
  24. }
  25. @Test
  26. public void testUpdate_ForSulfurasSellIn() throws Exception {
  27. int expectedSellIn = sulfuras.getSellIn();
  28. Item[] items = {sulfuras};
  29. Inventory sut = new Inventory(items);
  30. sut.updateQuality();
  31. assertEquals(expectedSellIn, sulfuras.getSellIn());
  32. }
  33. @Test
  34. public void testUpdate_ForNormalItemSellIn() throws Exception {
  35. int expectedSellIn = normalItem.getSellIn() - 1;
  36. Item[] items = {normalItem};
  37. Inventory sut = new Inventory(items);
  38. sut.updateQuality();
  39. assertEquals(expectedSellIn, normalItem.getSellIn());
  40. }
  41. @Test
  42. public void testUpdate_ForNormalItemQuantity() throws Exception {
  43. int expectedQuality = normalItem.getQuality() - 1;
  44. Item[] items = {normalItem};
  45. Inventory sut = new Inventory(items);
  46. sut.updateQuality();
  47. assertEquals(expectedQuality, normalItem.getQuality());
  48. }
  49. @Test
  50. public void testUpdate_ForSoldOutItem() throws Exception {
  51. int minimumQuality = 0;
  52. Item normalItemWithMinimumQuality = new Item(NORMAL_ITEM_NAME, 10, minimumQuality);
  53. Item[] items = {normalItemWithMinimumQuality};
  54. Inventory sut = new Inventory(items);
  55. sut.updateQuality();
  56. assertEquals(minimumQuality, normalItemWithMinimumQuality.getQuality());
  57. }
  58. @Test
  59. public void testUpdate_ForNormalItemOnceTheSellInDatePassed() throws Exception {
  60. Item normalItemWithPassedSellDate = new Item(NORMAL_ITEM_NAME, -1, 25);
  61. int expectedQuality = normalItemWithPassedSellDate.getQuality() - 2;
  62. Item[] items = {normalItemWithPassedSellDate};
  63. Inventory sut = new Inventory(items);
  64. sut.updateQuality();
  65. assertEquals(expectedQuality, normalItemWithPassedSellDate.getQuality());
  66. }
  67. @Test
  68. public void testUpdate_AgedBrie() throws Exception {
  69. Item agedBrie = new Item(AGED_BRIE_NAME, 10, 25);
  70. int expectedQuality = agedBrie.getQuality() + 1;
  71. Item[] items = {agedBrie};
  72. Inventory sut = new Inventory(items);
  73. sut.updateQuality();
  74. assertEquals(expectedQuality, agedBrie.getQuality());
  75. }
  76. @Test
  77. public void testUpdate_WhenAgedBrieIsAtMaxQuality() throws Exception {
  78. Item agedBrie = new Item(AGED_BRIE_NAME, 10, MAX_QUALITY);
  79. Item[] items = {agedBrie};
  80. Inventory sut = new Inventory(items);
  81. sut.updateQuality();
  82. assertEquals(MAX_QUALITY, agedBrie.getQuality());
  83. }
  84. }