Skip to content

Commit 8617137

Browse files
committed
Add unit test example to java-maven-quarkus
There are two tests, one that uses `@RestateTest` and one the uses `@QuarkusTest` with `RestateRunner`.
1 parent 4a08097 commit 8617137

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

java/templates/java-maven-quarkus/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ To start the service, simply run:
1212

1313
```shell
1414
$ quarkus dev
15-
```
15+
```
16+
17+
## Unit testing
18+
19+
The restate `RestateExtension` junit extension does currently not work together with quarkus CDI, so you cannot combine
20+
`@RestateTest` with `@QuarkusTest`, so if you want to use `@Inject` in unit test with restate services, you must use
21+
the `RestateRunner` directly as shown in the `GreeterCdiTest`.

java/templates/java-maven-quarkus/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,24 @@
3939
<groupId>io.quarkus</groupId>
4040
<artifactId>quarkus-vertx</artifactId>
4141
</dependency>
42+
<dependency>
43+
<groupId>io.quarkus</groupId>
44+
<artifactId>quarkus-junit5</artifactId>
45+
</dependency>
4246

4347
<dependency>
4448
<groupId>dev.restate</groupId>
4549
<artifactId>sdk-java-http</artifactId>
4650
<version>${restate.version}</version>
4751
</dependency>
4852

53+
<dependency>
54+
<groupId>dev.restate</groupId>
55+
<artifactId>sdk-testing</artifactId>
56+
<version>${restate.version}</version>
57+
<scope>test</scope>
58+
</dependency>
59+
4960
<!-- This is usually not needed, but fails without it with CNFE -->
5061
<dependency>
5162
<groupId>com.google.protobuf</groupId>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.acme;
2+
3+
import dev.restate.client.Client;
4+
import dev.restate.sdk.endpoint.Endpoint;
5+
import dev.restate.sdk.testing.RestateRunner;
6+
import io.quarkus.test.junit.QuarkusTest;
7+
import jakarta.inject.Inject;
8+
import org.junit.jupiter.api.AfterAll;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
14+
@QuarkusTest
15+
public class GreeterCdiTest {
16+
17+
@Inject
18+
Greeter greeter;
19+
20+
static RestateRunner restateRunner;
21+
22+
@BeforeEach
23+
void beforeEach() {
24+
if (restateRunner == null) {
25+
restateRunner = RestateRunner.from(Endpoint.builder().bind(greeter).build()).build();
26+
restateRunner.start();
27+
}
28+
}
29+
30+
@AfterAll
31+
static void afterAll() {
32+
if (restateRunner != null && restateRunner.getRestateContainer().isRunning()) {
33+
restateRunner.stop();
34+
}
35+
}
36+
37+
@Test
38+
void test() {
39+
var ingressClient = Client.connect(restateRunner.getRestateUrl().toString());
40+
var c = GreeterClient.fromClient(ingressClient);
41+
var response = c.greet(new Greeter.Greeting("John Doe"));
42+
assertEquals("You said hi to John Doe!", response.message());
43+
}
44+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.acme;
2+
3+
import dev.restate.client.Client;
4+
import dev.restate.sdk.testing.BindService;
5+
import dev.restate.sdk.testing.RestateClient;
6+
import dev.restate.sdk.testing.RestateTest;
7+
import org.acme.Greeter.GreetingResponse;
8+
import org.junit.jupiter.api.Test;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
@RestateTest
13+
public class GreeterTest {
14+
15+
@BindService
16+
Greeter greeter = new Greeter();
17+
18+
@Test
19+
void test(@RestateClient Client ingressClient) {
20+
GreeterClient.IngressClient c = GreeterClient.fromClient(ingressClient);
21+
GreetingResponse response = c.greet(new Greeter.Greeting("John Doe"));
22+
assertEquals("You said hi to John Doe!", response.message());
23+
}
24+
}

0 commit comments

Comments
 (0)