Junit for Consumer class returning lambda

30 views Asked by At

I am writing junit test case for below for consumer class which has below method.

@Bean 
@Transactional 
public Consumer<Message<String>> response() {  
    return message -> { 
        Acknowledgment ack = message.getHeaders().get(Ack, Acknowledgment.class);
        helper(message); 
        log.info("hello"); 
        ack.acknowlege(); 
    }

I am trying to go inside the lambda function through test method but able to proceed further. I tried to write the test as below:

@Test
public void testHelper() {
    Message<String> message = MessageBuilder.withPayload("Hello").build();
    consumer.response();
}
1

There are 1 answers

1
Naman On

Assuming that you are referencing java.util.function.Consumer, the response method is at present returning an instance of the Consumer type and is not invoking the implementation as shared.

The operations would be performed only once you call the accept method of the Consumer returned. Thereafter you can verify the occurrences of the methods invoked within.