Search code examples
scalaunit-testingapache-kafkajmockitkafka-consumer-api

Mocking a static method which is written in Scala in Java Unit test


We are using Kafka Clients in a project. I am trying to mock a static method from the Kafka client via JMockit :

    new NonStrictExpectations() {
        {
            new MockUp<Consumer>()
            {                   
                @Mock
                ConsumerConnector createJavaConsumerConnector(
                        ConsumerConfig c){
                    return null;
                }
            };
        }
    };

Looks like JMockit is not working due to some reason. I am sure of the syntax of JMockit for mocking static methods. This is the error :

java.lang.IllegalArgumentException: Matching real methods not found for the following mocks:
dispatcher.DispatcherTests$1$1#createJavaConsumerConnector(kafka.consumer.ConsumerConfig)

If this does not work because the Kafka client code is in Scala, how does my program work?


Solution

  • There are multiple ConsumerConnector classes - the one I was using was from the wrong package..Scala was not creating any problem here. It worked after using the right ConsumerConnector class.