Guest User

SupplySystem with test before refactoring

a guest
Mar 11th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.64 KB | None | 0 0
  1. // BEFORE REFACTORING:
  2.  
  3.  
  4. package org.legacy;
  5.  
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import org.mockito.Mockito;
  9.  
  10. public class SupplyMonitorTest {
  11.     private SupplyMonitor underTest;
  12.     private Inventory inventory = Mockito.mock(Inventory.class);
  13.  
  14.     @Before
  15.     public void setUp() throws Exception {
  16.         underTest = new SupplyMonitor(inventory, 100);
  17.     }
  18.  
  19.     @Test
  20.     public void should_order_pencils_when_undersupplied() {
  21.         Mockito.when(inventory.getItemCount(SupplyMonitor.PENCIL_ITEM_CODE)).thenReturn(120);
  22.         underTest.trackAndProcurePencils();
  23.  
  24.         // Oh, dear. Not only to I have no way to test what happened inside the
  25.         // method under test, but I think I just bought 280 pencils with my credit card!!!!
  26.     }
  27. }
  28.  
  29. //-------------------------------------------------------
  30.  
  31. package org.legacy;
  32.  
  33. public class SupplyMonitor {
  34.     private static final int TARGET_PENCILS_PER_EMPLOYEE = 4;
  35.     static final String PENCIL_ITEM_CODE = "123";
  36.     private static final String CREDIT_CARD_NUMBER = "XXXX XXXX XXXX XXXX";
  37.  
  38.     private Inventory inventory;
  39.     private int currentEmployeeCount;
  40.  
  41.     public SupplyMonitor(Inventory inventory, int currentEmployeeCount) {
  42.         super();
  43.         this.inventory = inventory;
  44.         this.currentEmployeeCount = currentEmployeeCount;
  45.     }
  46.  
  47.     public void trackAndProcurePencils() {
  48.         int targetPencilCount = this.currentEmployeeCount * TARGET_PENCILS_PER_EMPLOYEE;
  49.         int currentPencilCount = inventory.getItemCount(PENCIL_ITEM_CODE);
  50.         if (currentPencilCount < targetPencilCount) {
  51.             SupplySystem.buy(new PurchaseOrder(PENCIL_ITEM_CODE, targetPencilCount - currentPencilCount),
  52.                     new CreditCard(CREDIT_CARD_NUMBER));
  53.         }
  54.     }
  55.  
  56. }
Add Comment
Please, Sign In to add comment