1

I am using github.com/confluentinc/confluent-kafka-go/kafka as a package to deal with kafka.

It has functions like NewConsumer, SubscribeTopics, Poll which are not declared using interface. How do I mock them for unit testing?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • Why do you want to mock them? – Jonathan Hall Sep 20 '21 at 12:56
  • @Flimzy to test my code. These Functions are call in my other functions, and I need to test them – Krishna patil Sep 20 '21 at 13:13
  • Obviously to test your code. But what do you want to mock them? Why not just call those functions as they are? What is your reason for wanting to mock them? Most tests never need mocks. Why do you think this test does? – Jonathan Hall Sep 20 '21 at 13:16
  • So, lets say function Poll pulls kafka messages from topic, so i wanted to mock the Poll function, So I can fake this Poll messages using mock function, and after that continue my test with this fake messages. – Krishna patil Sep 20 '21 at 13:32
  • 1
    Why don't you update your question with an actual function that you need to test? You probably don't need "fake" messages at all. – Jonathan Hall Sep 20 '21 at 13:37

1 Answers1

-1

How do I Mock Them for unit testing?

You cannot. Redesign your test strategy.

Volker
  • 40,468
  • 7
  • 81
  • 87
  • Ok, I cannot mock them, so how do I Test my code now. – Krishna patil Sep 20 '21 at 13:12
  • 1
    My recommendation was to _not_ mock them (mocking is a bad technique and doesn't work well in Go if at all) but to redesign your test strategy: Test your code without a need for a mock. – Volker Sep 20 '21 at 13:22