We are building an application which will be using Kafka Streams. We are looking for sample example which shows us how to write a test case for a scenario, where we are calling an external service from Kafka topology. Basically that external call needs to be Mocked somehow, as service might not be running always. We are using TopologyTestDriver for writing test case. Due to this external call our test case is not executing. Getting error : org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8080/endPointName": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
Sample code for which we want to write test case:
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public void method(StreamsBuilder builder) {
builder.stream(inTopic,Consumed.with(StreamsSerdes.String(),new StreamsSerdes.CustomSerde()))
.peek((s, customObj) -> LOG.info(customObj))
.mapValues(this::getResult)
.peek((s, result) -> LOG.info(result))
.to(outputTopic,Produced.with(StreamsSerdes.String(),new ResultSerde()));
}
private Result getResult(Custom customObj) {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<Custom> request = new HttpEntity<>(customObj, headers);
return restTemplate.postForEntity(restCompleteUri, request, Result.class).getBody();
}
Sample Test Case Example:
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest
public class TopologyTest {
private static TopologyTestDriver topologyTestDriver;
private static final Logger LOG = LogManager.getLogger();
@Autowired
private ConfigurableApplicationContext appContext;
@BeforeAll
void setUp() {
Properties properties = getProperties();
StreamsBuilder builder = new StreamsBuilder();
appContext.getBean(PublisherSubscriberTopology.class).withBuilder(builder);
Topology topology = builder.build();
topologyTestDriver = new TopologyTestDriver(topology, properties);
}
private Properties getProperties() {
Properties properties = new Properties();
properties.put(StreamsConfig.APPLICATION_ID_CONFIG, "test");
properties.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "test:9092");
properties.put(StreamsConfig.DEFAULT_DESERIALIZATION_EXCEPTION_HANDLER_CLASS_CONFIG, LogDeserializationExceptionHandler.class.getName());
properties.put(StreamsConfig.DEFAULT_PRODUCTION_EXCEPTION_HANDLER_CLASS_CONFIG, CustomProductionExceptionHandler.class.getName());
return properties;
}
@Test
void testAppFlow() {
Custom customObj = getCustomObj();
Result result = getResult();
ConsumerRecordFactory<String, Custom> resultFactory =
new ConsumerRecordFactory<>(inTopic,
StreamsSerdes.String().serializer(), StreamsSerdes.CustomSerde().serializer());
topologyTestDriver.pipeInput(resultFactory.create(
inTopic,
"1001",
customObj
));
ProducerRecord<String, Result> record =
topologyTestDriver.readOutput(
outputTopic,
StreamsSerdes.String().deserializer(),
StreamsSerdes.ResultSerde().deserializer()
);
assertAll(() -> assertEquals("abc123", record.value().getABC()));
}
private Custom getCustomObj() {
Custom customObj = new Custom();
//setting customObj
return customObj;
}
private Result getResult() {
Result result = new Result();
//setting resultObj
return result;
}
@AfterAll
static void tearDown() {
try {
topologyTestDriver.close();
} catch (Exception e) {
LOG.error(e.getMessage());
}
}
}