5

I'm experiencing following problem. I've got a spring boot test, where I inject and spy the mongoDbChannel bean. Then I try to start the normal workflow and verify if the method send is called on the bean.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MongoAsBackupConfig.class},
        properties = {},
        webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class MongoAsBackupConfigTest {
    @SpyBean(name = "mongoDbChannel")
    private QueueChannel mongoDbChannel;

    @Autowired
    private DirectChannel mongoDbWithFailoverChannel;

    @DirtiesContext
    @Test
    public void shouldUseFallbackForFullQueue() throws InterruptedException {
        IntStream.rangeClosed(1, BACKUP_QUEUE_CAPACITY + OVERFILLING_CLICK_COUNT).forEach(someNumber ->
            mongoDbWithFailoverChannel.send(MessageBuilder.withPayload(createPayload(someNumber)).build()));
        verify(mongoDbChannel, times(BACKUP_QUEUE_CAPACITY)).send(Mockito.any());
    }
}

As a result, I get the error message that any doesn't match to the concrete parameter value. However normally any means any value of param. What went wrong here?

Argument(s) are different! Wanted:
mongoDbChannel.send(
    <any>
);
-> at MongoAsBackupConfigTest.shouldUseFallbackForFullQueue(MongoAsBackupConfigTest.java:67)
Actual invocation has different arguments:
mongoDbChannel.send(
    GenericMessage [payload=Click(...), headers={id=0eaa2317-b1b5-604d-65c5-78da521cd585, timestamp=1509085945379}],
    10
);
-> at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)

EDITED: I'm using java 8. And I tried to use any(GenericMessage.class), any(Message.class) but it was the same effect.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
Roman T
  • 1,400
  • 5
  • 18
  • 31

1 Answers1

2

I assume you are using java 8 which means that when using Mockito.any(), the compiler will infer the type that has to be used based on the parameter type in the signature of send method.

That seems to be Message based on the method definition : send(Message<?> message)

What is actually passed is an instance of GenericMessage.

As I assume GenericMessage extends Message, then you can write your verify as follows:

verify(mongoDbChannel, times(BACKUP_QUEUE_CAPACITY))
   .send(Mockito.any(GenericMessage.class));

Update

There also seems to be an overloaded method send(Message<?> message, long timeout). Maybe this version gets called instead of the single arg one..

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
  • yes it's java 8. Unfortunatelly you proposal doesn't work as well with the same error message. I tried also any(Message.class). – Roman T Oct 27 '17 at 07:14
  • yes, it does! It's visible in the error message `send( GenericMessage [], 10 );` It works now, thnx. – Roman T Oct 27 '17 at 07:30