Skip to main content

Testing

The Java SDK comes with the module sdk-testing that integrates with JUnit 5 and TestContainers to start up a Restate container together with your services code and automatically register them.

implementation("dev.restate:sdk-testing:1.2.0")

Using the JUnit 5 Extension

Given the service to test GreeterService, annotate the test class with @RestateTest and annotate the services to bind with @BindService:

@RestateTest
class GreeterTest {
@BindService GreeterService service = new GreeterService();
// Your tests
}

Note that the extension will start one Restate server for the whole test class. For more details, checkout RestateTest Javadocs.

Once the extension is set, you can implement your test methods as usual, and inject a Client using @RestateClient to interact with Restate and the registered services:

@Test
void testGreet(@RestateClient Client ingressClient) {
// Create the service client from the injected ingress client
var client = GreeterServiceClient.fromClient(ingressClient);
// Send request to service and assert the response
var response = client.greet("Francesco");
assertEquals(response, "Hello, Francesco!");
}

Usage without JUnit 5

You can use the testing tools without JUnit 5 by creating a ManualRestateRunner with RestateRunnerBuilder#buildManualRunner. For more details, refer to the Javadocs.