Reputation: 16525
I have some runnable where one of the parameter is delegator with taskExecutor to execute another runnable:
@Override
public void run() {
try {
doTask(messageId);
} catch (Exception e) {
count++;
if (count < 4) {
delegatedTransactionalAsyncTaskExecutor.execute(this);
} else {
delegatedTransactionalAsyncTaskExecutor.execute(getOnExceedErrorTask(messageId));
}
throw new RuntimeException(e);
}
}
how should I test this ?
Upvotes: 1
Views: 59
Reputation: 140427
It seems that delegatedTransactionalAsyncTaskExecutor
is a field in your class.
In order to ensure that you can test it, you have to use dependency injection, like this:
class UnderTest {
private final Whatever delegatedTransactionalAsyncTaskExecutor;
UnderTest(Whatever delegatedTransactionalAsyncTaskExecutor) {
this.delegatedTransactionalAsyncTaskExecutor = delegatedTransactionalAsyncTaskExecutor;
...
And now, you can use mocking frameworks to create objects of that Whatever class. Mocks allow you to specify the method calls you expect to happen; and then, you can verify later on that those calls really took place.
In other words: you prepare a mock; then you invoke run() ... and afterwards you check that those calls you were looking for actually happened. And of course for the whole thing to work, you must be able to inject those mocks into your "class under test".
Upvotes: 1