Guest User

SupplySystem with test after refactoring

a guest
Mar 11th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.61 KB | None | 0 0
  1. // AFTER REFACTORING:
  2.  
  3.  
  4. package org.legacy;
  5.  
  6. import org.hamcrest.Matcher;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import org.mockito.ArgumentMatcher;
  10. import org.mockito.Mockito;
  11.  
  12. public class SupplyMonitorTest {
  13.     private SupplyMonitor underTest;
  14.     private Inventory inventory = Mockito.mock(Inventory.class);
  15.     private SupplySystemWrapper supplySystem = Mockito.mock(SupplySystemWrapper.class);
  16.     private Matcher<PurchaseOrder> orderMatcher = new ArgumentMatcher<PurchaseOrder>() {
  17.         @Override
  18.         public boolean matches(Object argument) {
  19.             PurchaseOrder order = (PurchaseOrder) argument;
  20.             return SupplyMonitor.PENCIL_ITEM_CODE.equals(order.getItemCode())
  21.                     && order.getQuantity() == 280;
  22.         }      
  23.     };
  24.  
  25.     @Before
  26.     public void setUp() throws Exception {
  27.         underTest = new SupplyMonitor(inventory, 100, supplySystem);
  28.     }
  29.  
  30.  
  31.     @Test
  32.     public void should_order_pencils_when_undersupplied() {
  33.         Mockito.when(inventory.getItemCount(SupplyMonitor.PENCIL_ITEM_CODE)).thenReturn(120);
  34.         underTest.trackAndProcurePencils();
  35.         Mockito.verify(supplySystem).buy(Mockito.argThat(orderMatcher ), Mockito.any());
  36.     }
  37.    
  38. }
  39.  
  40.  
  41. //-------------------------------------------------------
  42.  
  43.  
  44. package org.legacy;
  45.  
  46. public class SupplyMonitor {
  47.     private static final int TARGET_PENCILS_PER_EMPLOYEE = 4;
  48.     static final String PENCIL_ITEM_CODE = "123";
  49.     private static final String CREDIT_CARD_NUMBER = "XXXX XXXX XXXX XXXX";
  50.     private SupplySystemWrapper supplySystem;
  51.  
  52.     private Inventory inventory;
  53.     private int currentEmployeeCount;
  54.  
  55.     // legacy constructor - to be deleted once all refererencing code has been
  56.     // refactored
  57.     public SupplyMonitor(Inventory inventory, int currentEmployeeCount) {
  58.         super();
  59.         this.inventory = inventory;
  60.         this.currentEmployeeCount = currentEmployeeCount;
  61.  
  62.         // one new line to keep the old static call via the wrapper
  63.         this.supplySystem = new SupplySystemWrapper() {
  64.         };
  65.     }
  66.  
  67.     // new constructor for tests
  68.     public SupplyMonitor(Inventory inventory, int currentEmployeeCount, SupplySystemWrapper supplySystem) {
  69.         super();
  70.         this.supplySystem = supplySystem;
  71.         this.inventory = inventory;
  72.         this.currentEmployeeCount = currentEmployeeCount;
  73.     }
  74.  
  75.     public void trackAndProcurePencils() {
  76.         int targetPencilCount = this.currentEmployeeCount * TARGET_PENCILS_PER_EMPLOYEE;
  77.         int currentPencilCount = inventory.getItemCount(PENCIL_ITEM_CODE);
  78.         if (currentPencilCount < targetPencilCount) {
  79.             // changed static call to wrapper call:
  80.             this.supplySystem.buy(new PurchaseOrder(PENCIL_ITEM_CODE, targetPencilCount - currentPencilCount),
  81.                     new CreditCard(CREDIT_CARD_NUMBER));
  82.         }
  83.     }
  84. }
Add Comment
Please, Sign In to add comment