0
votes

I am trying to write tests for my SWF workflow, but stuck with a problem, which I am guessing is because of the asynchronous code or Settable objects in my workflow.

My code looks like

@Asynchronous public Promise doSomething(final Object object,Promise success) {

    final Settable<Throwable> exception = new Settable<Throwable>();
    final Settable<Boolean> result = new Settable<Boolean>();

    new TryCatch() {

        @Override
        protected void doTry() throws Throwable {

                Promise<Boolean> waitFor = activitiesClient.doClientWork(object);
                result.chain(waitFor);
                setState(exception, null, waitFor);
        }

        @Override
        protected void doCatch(Throwable t) throws Throwable {

            result.set(false);
            setState(exception, t, Promise.Void());
        }
    };

    handleException(object, "Failed", exception);
    return result;  
}

When I run my test, the control never enters tryCatch and goes straight to handleException(..) line. I am guessing I am missing something about the Settable maybe?

As suggested on other forums, I tried both approaches 'WorkflowTest' as well as 'AsyncScope' but still can't get over similar problem while writing unit test for this workflow.

Please help.

My test code looks like

@InjectMocks
CustomWorkflow workflow = new CustomWorkflowImpl();

@Mock
MyActivitiesClient activitiesClient;

@Test
public void testSomething() throws Throwable {

    AsyncScope scope = new AsyncScope() {

        protected void doAsync() {

        workflow.doSomething(processTransaction);
        }
    };
    scope.eventLoop();

    // assert
    verify(activitiesClient, times(1)).doClientWork(processTransaction);
}

However I get an exception message 'java.lang.IllegalStateException: not ready' on handleException line.

Please note I am using @InjectMocks to instantiate workflow object and I am guessing I don't need aspectweaver.jar in the classpath for this? Hope thats correct.

1

1 Answers

0
votes

Code is asynchronous so handleException is expected to be called before doTry() is executed. But if it is marked with @Asynchronous then it shouldn't be executed until exception promise is ready. Make sure that @Asynchronous is actually setup correctly.

I would also change result.set(false) to if (!result.isReady()) { result.set(false); } to ensure that it doesn't throw if it is already set.