Forum Discussion

abhinaykumar15's avatar
abhinaykumar15
Occasional Contributor
6 months ago

Unable to read Kafka received messages using MessageExchange

Hi All,

I have subscribed to a topic and i receive messages in the topic.

When i add a script assertion to this step using MessageExchange like

def response = messageExchange.getResponse()
log.info response

I am getting the following error

Error in assertion script of the [Receive Message] test step: Cannot invoke method getResponse() on null object

Am i missing to add something here?Can i read kafka received messages using messageExchange.

Kindly help.

  • Hi All,

    From ReadyAPI 3.54 kafka received messages can be read using groovy script and can be validated.

    def data = context.expand( '${Teststep-name#data}' )

    log.info(data)

     

  • Humashankar's avatar
    Humashankar
    Champion Level 3

    Hi abhinaykumar15 

    You're on the right track using MessageExchange to access messages in ReadyAPI, but there's a refined difference when working with Kafka compared to traditional requests and responses.

    The "null object" error occurs because messageExchange.getResponse() is designed for retrieving responses in a request-response flow, which doesn't apply to subscribing to a Kafka topic.

    Try using a Groovy Script assertion in your "Receive Message" test step to access the received Kafka messages. Specifically, utilize messageExchange.getMessageBodies() to retrieve a list of messages, allowing you to effectively handle the Kafka message exchange.

    Hope this helps - Happy to help further!!
    Thank you very much and have a great one!
    Warm regards

  • abhinaykumar15's avatar
    abhinaykumar15
    Occasional Contributor

    Hi All,

    From ReadyAPI 3.54 kafka received messages can be read using groovy script and can be validated.

    def data = context.expand( '${Teststep-name#data}' )

    log.info(data)