I can’t figure out what I am doing wrong. My tests work fine when mocking happy path, but when throwing an error on a mocked activity the test hangs indefinitely. I tried different combinations of retry configurations, and setting setFailWorkflowExceptionTypes
for the workflow.
import io.temporal.client.WorkflowException
import io.temporal.client.WorkflowOptions
import io.temporal.failure.ActivityFailure
import io.temporal.failure.ApplicationFailure
import io.temporal.failure.ChildWorkflowFailure
import io.temporal.testing.TestWorkflowEnvironment
import io.temporal.testing.TestWorkflowExtension
import io.temporal.worker.Worker
import io.temporal.worker.WorkflowImplementationOptions
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension
import org.mockito.ArgumentMatchers.anyString
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
class HelloWorldWorkflowImplTest {
private val taskQueue = "TASK_QUEUE"
private val testEnv = TestWorkflowEnvironment.newInstance();
private val worker = testEnv.newWorker(taskQueue);
private val opts = WorkflowImplementationOptions.newBuilder()
.setFailWorkflowExceptionTypes(
Throwable::class.java,
)
// .setDefaultActivityOptions(
// ActivityOptions
// .newBuilder()
// .setStartToCloseTimeout(Duration.ofSeconds(1))
// .setRetryOptions(
// RetryOptions.newBuilder()
// .setMaximumAttempts(1)
// .setInitialInterval(Duration.ofMillis(10))
// .setBackoffCoefficient(1.0)
// .build()!!
// ).build()
// )
// .setDefaultLocalActivityOptions(
// LocalActivityOptions.newBuilder()
// .setStartToCloseTimeout(Duration.ofSeconds(1))
// .setRetryOptions(
// RetryOptions.newBuilder()
// .setMaximumAttempts(1)
// .setInitialInterval(Duration.ofMillis(10))
// .setBackoffCoefficient(1.0)
// .build()!!
// ).build()
// )
.build()!!
@Test
fun testMockedExceptionGetGreeting() {
val formatActivities = mock<HelloWorldActivities>()
whenever(formatActivities.composeGreeting(anyString())).thenThrow(NullPointerException("test error"))
worker.registerActivitiesImplementations(formatActivities)
worker.registerWorkflowImplementationTypes(
opts,
HelloWorldWorkflowImpl::class.java
)
testEnv.start()
val workflow = testEnv.workflowClient.newWorkflowStub(
HelloWorldWorkflow::class.java,
WorkflowOptions.newBuilder().setTaskQueue(taskQueue).build()!!
)
try {
workflow.getGreeting("Mock")
error("unreachable")
} catch (e: WorkflowException) {
assertTrue(e.cause is ChildWorkflowFailure)
assertTrue(e.cause?.cause is ActivityFailure)
assertTrue(e.cause?.cause?.cause is ApplicationFailure)
assertEquals(
"test error",
(e.cause?.cause?.cause as ApplicationFailure).originalMessage
)
}
}
}
package versions:
implementation("io.temporal:temporal-sdk:1.22.3")
testImplementation("io.temporal:temporal-testing:1.22.3")
testImplementation(platform("org.junit:junit-bom:5.10.1"))
testImplementation("org.junit.jupiter:junit-jupiter")