VisitorManager { public Visit visit(String ipAddress, String agentType) { try { return doVisit(ipAddress, agentType); } catch (ExecutionException ex) { throw new TennisStatsException("Error tracking visit.", Optional.ofNullable(ex.getCause()).orElse(ex)); } catch (Exception ex) { throw new TennisStatsException("Error tracking visit.", ex); } }  @PostConstruct void init(); @PreDestroy void destroy(); Visit visit(String ipAddress, String agentType);  }
@Test void secondVisitDoesNothing() { String ipAddress = "192.168.1.1"; visitAndVerifyFirstVisit(ipAddress); Visit visit = manager.visit(ipAddress, WEB_BROWSER.name()); assertThat(visit.visitor.getHits()).isEqualTo(2); verifyNoMoreInteractions(repository); verifyNoMoreInteractions(geoIPService); }
@Test void thirdVisitSavesVisitor() { setField(manager, "saveEveryHitCount", 3); String ipAddress = "192.168.1.1"; visitAndVerifyFirstVisit(ipAddress); manager.visit(ipAddress, WEB_BROWSER.name()); Visit visit = manager.visit(ipAddress, WEB_BROWSER.name()); assertThat(visit.visitor.getHits()).isEqualTo(3); verify(repository).save(visitorCaptor.capture()); assertThat(visitorCaptor.getValue().getIpAddress()).isEqualTo(ipAddress); verifyNoMoreInteractions(repository); }