### Question:
PortfolioEventHandler { @QueryHandler public PortfolioView find(PortfolioByIdQuery query) { String portfolioId = query.getPortfolioId().getIdentifier(); return Optional.ofNullable(portfolioViewRepository.getOne(portfolioId)) .orElseGet(() -> { logger.warn("Tried to retrieve a Portfolio query model with a non existent portfolio id [{}]", portfolioId); return null; }); } PortfolioEventHandler(PortfolioViewRepository portfolioViewRepository,
                                 OrderBookViewRepository orderBookViewRepository); @EventHandler void on(PortfolioCreatedEvent event); @EventHandler void on(ItemsAddedToPortfolioEvent event); @EventHandler void on(ItemsReservedEvent event); @EventHandler void on(ItemReservationConfirmedForPortfolioEvent event); @EventHandler void on(ItemReservationCancelledForPortfolioEvent event); @EventHandler void on(CashDepositedEvent event); @EventHandler void on(CashWithdrawnEvent event); @EventHandler void on(CashReservedEvent event); @EventHandler void on(CashReservationCancelledEvent event); @EventHandler void on(CashReservationConfirmedEvent event); @QueryHandler PortfolioView find(PortfolioByIdQuery query); @QueryHandler PortfolioView find(PortfolioByUserIdQuery query);  }### Answer:
@Test public void testFindByPortfolioIdReturnsAPortfolioView() { PortfolioView testView = buildTestPortfolio(); when(portfolioViewRepository.getOne(portfolioId.getIdentifier())).thenReturn(testView); PortfolioView result = testSubject.find(new PortfolioByIdQuery(portfolioId)); assertEquals(testView, result); }
@Test public void testFindByPortfolioIdReturnsNullForNonExistentPortfolioId() { when(portfolioViewRepository.getOne(portfolioId.getIdentifier())).thenReturn(null); assertNull(testSubject.find(new PortfolioByIdQuery(portfolioId))); }
@Test public void testFindPortfolioByUserIdReturnsAPortfolioView() { PortfolioView testView = buildTestPortfolio(); when(portfolioViewRepository.findByUserId(userId.getIdentifier())).thenReturn(testView); PortfolioView result = testSubject.find(new PortfolioByUserIdQuery(userId)); assertEquals(testView, result); }
@Test public void testFindPortfolioByUserIdReturnsNullForNonExistentUserId() { when(portfolioViewRepository.findByUserId(userId.getIdentifier())).thenReturn(null); assertNull(testSubject.find(new PortfolioByUserIdQuery(userId))); }