Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BEFORE REFACTORING:
- package org.legacy;
- import org.junit.Before;
- import org.junit.Test;
- import org.mockito.Mockito;
- public class SupplyMonitorTest {
- private SupplyMonitor underTest;
- private Inventory inventory = Mockito.mock(Inventory.class);
- @Before
- public void setUp() throws Exception {
- underTest = new SupplyMonitor(inventory, 100);
- }
- @Test
- public void should_order_pencils_when_undersupplied() {
- Mockito.when(inventory.getItemCount(SupplyMonitor.PENCIL_ITEM_CODE)).thenReturn(120);
- underTest.trackAndProcurePencils();
- // Oh, dear. Not only to I have no way to test what happened inside the
- // method under test, but I think I just bought 280 pencils with my credit card!!!!
- }
- }
- //-------------------------------------------------------
- package org.legacy;
- public class SupplyMonitor {
- private static final int TARGET_PENCILS_PER_EMPLOYEE = 4;
- static final String PENCIL_ITEM_CODE = "123";
- private static final String CREDIT_CARD_NUMBER = "XXXX XXXX XXXX XXXX";
- private Inventory inventory;
- private int currentEmployeeCount;
- public SupplyMonitor(Inventory inventory, int currentEmployeeCount) {
- super();
- this.inventory = inventory;
- this.currentEmployeeCount = currentEmployeeCount;
- }
- public void trackAndProcurePencils() {
- int targetPencilCount = this.currentEmployeeCount * TARGET_PENCILS_PER_EMPLOYEE;
- int currentPencilCount = inventory.getItemCount(PENCIL_ITEM_CODE);
- if (currentPencilCount < targetPencilCount) {
- SupplySystem.buy(new PurchaseOrder(PENCIL_ITEM_CODE, targetPencilCount - currentPencilCount),
- new CreditCard(CREDIT_CARD_NUMBER));
- }
- }
- }
Add Comment
Please, Sign In to add comment