the hand of NOD
the hand of NOD

Reputation: 1769

JUnit Test for Apache Camel Route with Mockito Mock does not fail

I wrote a Camel-Route which uses the DLC-Pattern with an Processor which is executed before the Exchange is sent to the DLC.

errorHandler(deadLetterChannel("{{myDLCEndpoint}}")
                .onPrepareFailure(getErrorProcessor()));

During my Route I throw a RuntimeException which is then handled by the errorProcessor and the DLC. Everything works as expected when I start the application and let the route run.

Now I wanted to write a Unit-Test just be sure that it works.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@RunWith(SpringRunner.class)
public class MyRouteTest {
@MockBean
    private ErrorProcessor errorProcessor;

    @EndpointInject(uri = "{{quelle}}")
    private ProducerTemplate quelle;

    @EndpointInject(uri = "{{myDLCEndpoint}}")
    private ProducerTemplate dlc;

    @Test
    @Transactional("myDataSourceTransactionManager") //For Rollback
    public void test() throws Exception {
        Mockito.verify(errorProcessor, never()).process(Mockito.any());

        String inputXML = TestDataReader.readXML("myfile.xml");
        assertNotNull(inputXML);

        quelle.sendBody(inputXML);
    }
}

I started the test and checked the log and unfortunately an exception occurs during the route is executed. The exception is handled by camel and the mocked errorprocessor is called for sure because I debugged it:

debugger: the mocked bean is called for sure!

Unfortunately the unit test still succeeds even with Mockito.verify(errorProcessor, never()).process(Mockito.any());

And now I have no clue why it does not fail, which would be the result that I'd expect in such a situation?

Upvotes: 0

Views: 2275

Answers (1)

the hand of NOD
the hand of NOD

Reputation: 1769

I'm an idiot. The call of Mockito.verify was before the call quelle.sendBody. WAAAH. Sorry guys, I just didn't see it :-D

The correct way to get Mockito to work is:

        String inputXML = TestDataReader.readXML("myfile.xml");
        assertNotNull(inputXML);

        quelle.sendBody(inputXML);
        //call mockito AFTER the test is executed! 
        Mockito.verify(errorProcessor, never()).process(Mockito.any());

Upvotes: 4

Related Questions