有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java需要但没有被调用:<Capturing argument>实际上,与这个mock没有任何交互

我使用的是spring boot、maven、junit4,测试代码如下,当我运行此测试时,总是会出现以下错误:

Wanted but not invoked: onSubscriptionEventListener.handleSubscriptionEvent( ); -> at com.sap.ngom.urm.contract.consumer.VerifySubscriptionKafkaEventContractTest.handle_subscription_deleted_event(VerifySubscriptionKafkaEventContractTest.java:142) Actually, there were zero interactions with this mock.

Wanted but not invoked: onSubscriptionEventListener.handleSubscriptionEvent( ); -> at com.sap.ngom.urm.contract.consumer.VerifySubscriptionKafkaEventContractTest.handle_subscription_deleted_event(VerifySubscriptionKafkaEventContractTest.java:142) Actually, there were zero interactions with this mock.

@SpringBootTest(classes = {DummyApplication.class},
        properties = {"stubrunner.kafka.enabled=true","stubrunner.stream.enabled=false","spring.kafka.bootstrap-servers: ${spring.embedded.kafka.brokers}"})
@AutoConfigureStubRunner(ids = {"com.xxx.contracts:+:stubs"}, consumerName = "urm")
@EmbeddedKafka(partitions = 1, topics = {xxx_TOPIC})
@ActiveProfiles({"contracttest"})
@RunWith(SpringRunner.class)
public class VerifyKafkaEventContractTest {

    @Autowired
    StubTrigger stubTrigger;

    @SpyBean
    OnSubscriptionEventListener onSubscriptionEventListener;

    @Captor
    ArgumentCaptor<SubscriptionEvent> subscriptionArgumentCaptor;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        Mockito.doNothing().when(onSubscriptionEventListener).handleSubscriptionEvent(any());
 

    }

    @Test
    public void handle_subscription_deleted_event() {

        stubTrigger.trigger("kafka-subscription-deletion-urm");

 verify(onSubscriptionEventListener).handleSubscriptionEvent(subscriptionArgumentCaptor.capture());
            }
     }  

这可能是@SpyBean的bug吗


共 (1) 个答案

  1. # 1 楼答案

    我的服务还有其他侦听器,它们使用spring云流来使用rabbitmq事件。因此,我使用spring cloud stream实现侦听器,而不是使用@kafkaListener。然后合同测试工作。 假设这是与kafka一起使用spring云流时的一个bug