我有一个spring
带有@KafkaListener
方法的组件:
@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);
}
}
现在,我想测试这个方法。我想确保此方法正确接收消息。我尝试创建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() {
}
}
但我不明白接下来会发生什么。我该如何测试这种方法?