Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion java/templates/java-maven-quarkus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ To start the service, simply run:

```shell
$ quarkus dev
```
```

## Unit testing

The restate `RestateExtension` junit extension does currently not work together with quarkus CDI, so you cannot combine
`@RestateTest` with `@QuarkusTest`, so if you want to use `@Inject` in unit test with restate services, you must use
the `RestateRunner` directly as shown in the `GreeterCdiTest`.
11 changes: 11 additions & 0 deletions java/templates/java-maven-quarkus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,24 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
</dependency>

<dependency>
<groupId>dev.restate</groupId>
<artifactId>sdk-java-http</artifactId>
<version>${restate.version}</version>
</dependency>

<dependency>
<groupId>dev.restate</groupId>
<artifactId>sdk-testing</artifactId>
<version>${restate.version}</version>
<scope>test</scope>
</dependency>

<!-- This is usually not needed, but fails without it with CNFE -->
<dependency>
<groupId>com.google.protobuf</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.acme;

import dev.restate.client.Client;
import dev.restate.sdk.endpoint.Endpoint;
import dev.restate.sdk.testing.RestateRunner;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

@QuarkusTest
public class GreeterCdiTest {

@Inject
Greeter greeter;

static RestateRunner restateRunner;

@BeforeEach
void beforeEach() {
if (restateRunner == null) {
restateRunner = RestateRunner.from(Endpoint.builder().bind(greeter).build()).build();
restateRunner.start();
}
}

@AfterAll
static void afterAll() {
if (restateRunner != null && restateRunner.getRestateContainer().isRunning()) {
restateRunner.stop();
}
}

@Test
void test() {
var ingressClient = Client.connect(restateRunner.getRestateUrl().toString());
var c = GreeterClient.fromClient(ingressClient);
var response = c.greet(new Greeter.Greeting("John Doe"));
assertEquals("You said hi to John Doe!", response.message());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.acme;

import dev.restate.client.Client;
import dev.restate.sdk.testing.BindService;
import dev.restate.sdk.testing.RestateClient;
import dev.restate.sdk.testing.RestateTest;
import org.acme.Greeter.GreetingResponse;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

@RestateTest
public class GreeterTest {

@BindService
Greeter greeter = new Greeter();

@Test
void test(@RestateClient Client ingressClient) {
GreeterClient.IngressClient c = GreeterClient.fromClient(ingressClient);
GreetingResponse response = c.greet(new Greeter.Greeting("John Doe"));
assertEquals("You said hi to John Doe!", response.message());
}
}
Loading