Reputation: 7084
I have a spring
component with @KafkaListener
method:
@Slf4j
@Component
public class ResponseHandler {
private final ResponseMessageService responseMessageService;
public ResponseHandler(ResponseMessageService responseMessageService) {
this.responseMessageService= responseMessageService;
}
@KafkaListener(topics = "response-topic", groupId = "response-group")
public void listen(ResponseMessage responseMessage) {
responseMessageService.processResponse(responseMessage);
}
}
Now, I want to test this method. I want to make sure that this method receives messages correct. I try to create a Unit test
:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ResponseHandlerTest {
@ClassRule
public static EmbeddedKafkaRule broker = new EmbeddedKafkaRule(1, false, 5, "response-topic");
@BeforeClass
public static void setup() {
System.setProperty("spring.kafka.bootstrap-servers", broker.getEmbeddedKafka().getBrokersAsString());
}
@Test
public void listen() {
}
}
But I don't understand what next. how can I test this method?
Upvotes: 1
Views: 4803
Reputation: 174504
See this answer for one way to do it.
Also, read Artem Bilan's answer on that same question.
Finally, you can replace your ResponseMessageService
with a mock object in your test case, and verify it was called as expected.
Upvotes: 1