Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable use mockito to verify if Emitter::send() was called with the correct parameter #165

Open
shyamsathyanathan opened this issue Mar 20, 2024 · 0 comments

Comments

@shyamsathyanathan
Copy link

How to replicate

I have simple Kafka Producer like so

@ApplicationScoped
public class CustomizationProducer {

  Emitter<KafkaRecord<String, MyObject>> emitter;

  @Inject
  @Channel("customized-feed")
  public CustomizationProducer(Emitter<KafkaRecord<String, MyObject>> emitter) {
    this.emitter = emitter;
  }

  public void produce(MyObject data, String partitionKey) {
    KafkaRecord<String, MyObject> record = KafkaRecord.of(partitionKey, data);
    Message<KafkaRecord<String, MyObject>> message = Message.of(record);
    emitter.send(message);
  }
}

I would like to test this class using Mockito like so

public class CustomizationProducerTest {

  private CustomizationProducer customizationProducer;
  private Emitter<KafkaRecord<String, MyObject>> mockEmitter;

  @BeforeEach
  void setup() {
    mockEmitter = mock(Emitter.class);
    customizationProducer = new CustomizationProducer(mockEmitter);
  }

  @Test
  void shouldSendCustomizedFeedWithPartitionKey() {
    // Arrange
    var myObject = MockDataUtil.buildMyObject();
    var partitionKey = "xyz";
    var kafkaRecord = KafkaRecord.of(partitionKey, myObject);
    var expectedFeed = Message.of(kafkaRecord);

    // Act
    customizationProducer.produce(myObject, partitionKey);

    // Assert
    verify(mockEmitter).send(eq(expectedFeed));  <-- This fails because Message does not implement equals method correctly
  }

}

This test fails with the following stacktrace

Argument(s) are different! Wanted:
emitter.send(
    org.eclipse.microprofile.reactive.messaging.Message$$Lambda$494/0x000000012d2794a8@393881f0
);
-> at some.package.CustomizationProducerTest.shouldSendCustomizedFeedWithPartitionKey(CustomizationProducerTest.java:39)
Actual invocations have different arguments:
emitter.send(
    org.eclipse.microprofile.reactive.messaging.Message$$Lambda$494/0x000000012d2794a8@4af46df3
);

As you can see, the test is failing because the equals method is not properly implemented in Message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant