我如何使用mockito-kotlin来检查ProducerFactory?

问题描述 投票:0回答:1

我在 Kotlin 上有一小段代码,其中配置了 Producer。

testImplementation(kotlin("test"))
testImplementation("org.mockito.kotlin:mockito-kotlin:4.1.0")
  • 属性
@ConfigurationProperties("initial")
data class KafkaProperties (

    @field:NotEmpty
    var topic: String = "topic",

    @field:NestedConfigurationProperty
    val kafka: KafkaProperties = KafkaProperties()
)
  • 记录
class Event (val id: Long) {
}
  • 制作人
@Configuration
@EnableConfigurationProperties(KafkaProperties::class)
class KafkaConfiguration (
    private val properties: KafkaProperties
) {

    @Bean
    fun initialProducerFactory(customizers: ObjectProvider<DefaultKafkaProducerFactoryCustomizer>):
            ProducerFactory<String,Event> {

        val producerProperties = this.properties.kafka.buildProducerProperties()

        val factory =
            buildFactory(producerProperties)

        customizers.orderedStream()
            .forEach {
                    customizer -> customizer.customize(factory)
            }

        return factory
    }

    private fun buildFactory(producerProperties: Map<String, Any>):
            DefaultKafkaProducerFactory<String, Event> {

        val factory = DefaultKafkaProducerFactory<String, Event>(producerProperties)

        factory.keySerializer = StringSerializer()

        val objectMapper = JacksonUtils.enhancedObjectMapper().disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
        val jsonSerializer = JsonSerializer<Event>(objectMapper)

        factory.valueSerializer = jsonSerializer

        val transactionIdPrefix = this.properties.kafka.producer.transactionIdPrefix
        if (transactionIdPrefix != null) {
            factory.setTransactionIdPrefix(transactionIdPrefix)
        }
        return factory
    }
}
  • 服务
@Service
class ServiceImpl(
    private val kafkaProperties: KafkaProperties,
    private val producerFactory: ProducerFactory<String, Event>
) : Service {
  private fun runPublishEventToKafka(Long id){

        val eventRequest = Event(id)
        val topic = kafkaProperties.topic

  val producer =  this.producerFactory.createProducer()
 
 producer.send(ProducerRecord(topic, eventRequest))
}
}

我在考试课上做了一个小空白

class ServiceImplTest {
  
    private val kafkaProperties: KafkaProperties = mock()
    private val producerFactory: ProducerFactory<String, Event> = mock()

   
    @BeforeEach
    fun setUp() {
        

        val producerFactory =
            mock<ProducerFactory<String, Event>>()

        val eventRequest = Event(1L)
        val topicTest = "topic.test"

        val producer = producerFactory.createProducer()
        val send = producer.send(ProducerRecord(topicTest, eventClosureRequest))

        whenever(
            producerFactory.createProducer(any())

        ).thenReturn(mock(send)) //fail
    }
    
    @Test
   fun test(){
     //....
   }

谁对如何制作调用方法的存根有任何想法 -

生产者.发送(...)

kotlin mockito spring-kafka
1个回答
0
投票

如果您想要存根,则需要使用一系列模拟,因为您使用一系列

ProducerFactory
->
Producer
->
send
ProducerFactory
Producer
都需要被嘲笑。设置中的一个问题是您在模拟的
producerFactory.createProducer()
上调用
producerFactory
,默认情况下应该返回
null

这就是我的设置方式

val producerFactory = mock<ProducerFactory<String, Event>>()
val mockProducer = mock<Producer<String, Event>()
  whenever(mockProducerFactory.createProducer()).thenReturn(mockProducer)

您不应在设置中直接调用

producerFactory.createProducer()
producer.send

您没有指定

send
的签名,所以我将其留空,但如果不返回任何内容,上面的内容应该是有效的。如果您想验证它是否被调用,那么您要做的就是

verify(mockProducer).send(any())

预先警告,您没有提供大量测试代码,并且您仍然负责如何将模拟

ProducerFactory
注入到您正在测试的生产代码中。

© www.soinside.com 2019 - 2024. All rights reserved.