nazerke
4/11/2019 - 3:57 AM

argument_captor

	@Test
	public void test_openNewOrder_success() throws Exception {
		
		// Setup
		Mockito.when(mockOrderDao.insert(Mockito.any(OrderEntity.class)))
		.thenReturn(1);
		
		// Execution
		String orderNumber = this.target.openNewOrder(CUSTOMER_ID);
		
		// Verification
		ArgumentCaptor<OrderEntity> orderEntityCaptor =
				ArgumentCaptor.forClass(OrderEntity.class);
		Mockito.verify(mockOrderDao).insert(orderEntityCaptor.capture());
		
		OrderEntity capturedOrderEntity = orderEntityCaptor.getValue();
		
		Assert.assertNotNull(capturedOrderEntity);
		Assert.assertEquals(CUSTOMER_ID, capturedOrderEntity.getCustomerId());
		Assert.assertEquals(orderNumber, capturedOrderEntity.getOrderNumber());
	}
	
	public class OrderServiceImpl implements OrderService {
		@Override
	public List<OrderSummary> getOrderSummary(long customerId)
			throws ServiceException {
		
		// Goal - interact with the dao to gather entities and 
		// create summary domain objects
		
		List<OrderSummary> resultList = new LinkedList<>();
		
		try {
			List<OrderEntity> orderEntityList = this.orderDao.findOrdersByCustomer(customerId);
			
			for (OrderEntity currentOrderEntity : orderEntityList) {
				
				OrderSummary orderSummary = this.transformer.transform(currentOrderEntity);
				resultList.add(orderSummary);
			}
			
		} catch (DataAccessException e) {
			// You should log the error
			throw new ServiceException("Data access error occurred", e);
		}
		
		return resultList;
	}
	}