EventKafkaPublisher implements EventPublisher { @Override public void publish(Event event) { producer = createProducer(); ProducerRecord<String, String> producerRecord = new ProducerRecord<>(destination.topic(), JSON.toJSONString(event)); producer.send(producerRecord); } EventKafkaPublisher(Destination destination); @Override void publish(Event event);  }
@Test public void should_send_CandidateNominated_application_events() { Destination destination = new Destination("localhost", 9012, "test"); EventKafkaMockPublisher mockPublisher = new EventKafkaMockPublisher(destination); CandidateNominated candidateNominated1 = new CandidateNominated("nominate ticket", "201912101000", "201001010110"); CandidateNominated candidateNominated2 = new CandidateNominated("nominate ticket again", "201912101000", "201001010110"); mockPublisher.publish(candidateNominated1); mockPublisher.publish(candidateNominated2); List<ProducerRecord<String, String>> actualHistory = mockProducer.history(); List<ProducerRecord<String, String>> expectedHistory = Arrays.asList( new ProducerRecord<>("test", JSON.toJSONString(candidateNominated1)), new ProducerRecord<>("test", JSON.toJSONString(candidateNominated2)) ); assertThat(actualHistory).isEqualTo(expectedHistory); }