@Test void shouldIncludeRandomIdAndCurrentDateTime() { when(orderIdGenerator.generateOrderId()).thenReturn("8d8b30e3-de52-4f1c-a71c-9905a8043dac"); LocalDateTime defaultLocalDateTime = LocalDateTime.of(2020, 1, 1, 12, 0); Clock fixedClock = Clock.fixed(defaultLocalDateTime.toInstant(ZoneOffset.UTC), ZoneId.of("UTC")); when(clock.instant()).thenReturn(fixedClock.instant()); when(clock.getZone()).thenReturn(fixedClock.getZone()); Order result = cut.createOrder("MacBook Pro", 2L, null); assertEquals("8d8b30e3-de52-4f1c-a71c-9905a8043dac", result.getId()); assertEquals(defaultLocalDateTime, result.getCreationDate()); }
public Order createOrder(String productName, Long amount, String parentOrderId) { Order order = new Order(); order.setId(parentOrderId == null ? orderIdGenerator.generateOrderId() : parentOrderId); order.setCreationDate(LocalDateTime.now(clock)); order.setAmount(amount); order.setProductName(productName); return order; }
OrderServiceRefactored { public Order createOrder(String productName, Long amount, String parentOrderId) { Order order = new Order(); order.setId(parentOrderId == null ? orderIdGenerator.generateOrderId() : parentOrderId); order.setCreationDate(LocalDateTime.now(clock)); order.setAmount(amount); order.setProductName(productName); return order; } }
OrderServiceRefactored { public Order createOrder(String productName, Long amount, String parentOrderId) { Order order = new Order(); order.setId(parentOrderId == null ? orderIdGenerator.generateOrderId() : parentOrderId); order.setCreationDate(LocalDateTime.now(clock)); order.setAmount(amount); order.setProductName(productName); return order; } OrderServiceRefactored(Clock clock, OrderIdGenerator orderIdGenerator); }
OrderServiceRefactored { public Order createOrder(String productName, Long amount, String parentOrderId) { Order order = new Order(); order.setId(parentOrderId == null ? orderIdGenerator.generateOrderId() : parentOrderId); order.setCreationDate(LocalDateTime.now(clock)); order.setAmount(amount); order.setProductName(productName); return order; } OrderServiceRefactored(Clock clock, OrderIdGenerator orderIdGenerator); Order createOrder(String productName, Long amount, String parentOrderId); }
OrderServiceRefactored { public Order createOrder(String productName, Long amount, String parentOrderId) { Order order = new Order(); order.setId(parentOrderId == null ? orderIdGenerator.generateOrderId() : parentOrderId); order.setCreationDate(LocalDateTime.now(clock)); order.setAmount(amount); order.setProductName(productName); return order; } OrderServiceRefactored(Clock clock, OrderIdGenerator orderIdGenerator); Order createOrder(String productName, Long amount, String parentOrderId);  }