Getting functional unit tests to wait until devkit

2019-08-28 17:55发布

问题:

I often have a requirement to wait some async process is finished in my devKit connector (which then causes isConnected to return true )

@ValidateConnection
public boolean isConnected() {
    return isConnected;
}

How can I get my functional unit test to wait until this is completed. I would have thought the unit test would have waited until all the connectors in my flow were "connected".

Atm I am using a sleep in the unit test to get round this in my FunctionalTestCase

Thread.sleep(5000);

assertEquals(1, targetEngineApplication.getNumberOfMessagesReveived());

Edit_____________________

the connect code:

@Connect
public void connectMethod(final String configFileLocation) throws ConnectionException {

    System.out.println("Connect called with config:" + configFileLocation);
    try {
        Engine.doInitialise(configFileLocation);
        Engine.doStart();
    } catch (InitialisationException e) {
        throw new ConnectionException(ConnectionExceptionCode.UNKNOWN, "0", e.getCause().getMessage(), e);
    } catch (StartingException e) {
        throw new ConnectionException(ConnectionExceptionCode.UNKNOWN, "0", e.getCause().getMessage(), e);
    }
    // this will also tell the unit tests to start
    isConnected = true;
}

回答1:

It's the case: when the functional test starts, everything that needs to be initialized and started is.

The notion of "connected" for DevKit connectors is different: connection happens on demand when a message processor is used.

So what do you want to wait for in your test: that an initialization sequence of the connector has been done or that a particular processor method has been executed?

For the record, chapter 12 of Mule in Action covers testing techniques that allow dealing with asynchronous scenarios.