-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19498e7
commit 0036cef
Showing
8 changed files
with
139 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package develop; | ||
|
||
import dev.restate.sdk.testing.RestateRunner; | ||
import dev.restate.sdk.testing.RestateRunnerBuilder; | ||
import dev.restate.sdk.testing.RestateClient; | ||
import develop.clients.GreeterService; | ||
import develop.clients.GreeterServiceClient; | ||
import dev.restate.sdk.client.Client; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class GreeterTest { | ||
|
||
// <start_extension> | ||
@RegisterExtension | ||
private static final RestateRunner RESTATE_RUNNER = | ||
RestateRunnerBuilder.create() | ||
// The service to test | ||
.bind(new GreeterService()) | ||
.buildRunner(); | ||
// <end_extension> | ||
|
||
// <start_test> | ||
@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!"); | ||
} | ||
// <end_test> | ||
} |
2 changes: 1 addition & 1 deletion
2
code_snippets/java/src/main/java/develop/clients/GreetCounterObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
code_snippets/java/src/main/java/develop/clients/GreeterService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
code_snippets/kotlin/src/main/kotlin/develop/GreeterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package develop | ||
|
||
import dev.restate.sdk.client.Client | ||
import dev.restate.sdk.testing.RestateClient | ||
import dev.restate.sdk.testing.RestateRunner | ||
import dev.restate.sdk.testing.RestateRunnerBuilder | ||
import develop.clients.GreeterService | ||
import develop.clients.GreeterServiceClient | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.RegisterExtension | ||
|
||
internal class GreeterTest { | ||
|
||
// <start_extension> | ||
companion object { | ||
@RegisterExtension | ||
private val RESTATE_RUNNER: RestateRunner = RestateRunnerBuilder.create() | ||
// The service to test | ||
.bind(GreeterService()) | ||
.buildRunner() | ||
} | ||
// <end_extension> | ||
|
||
// <start_test> | ||
@Test | ||
fun testGreet(@RestateClient ingressClient: Client) = runTest { | ||
// Create the service client from the injected ingress client | ||
val client = GreeterServiceClient.fromClient(ingressClient) | ||
|
||
// Send request to service and assert the response | ||
val response = client.greet("Francesco") | ||
assertEquals(response, "Hello, Francesco!") | ||
} | ||
// <end_test> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
sidebar_position: 14 | ||
description: "Test your services." | ||
--- | ||
|
||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
# 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. | ||
|
||
```kotlin | ||
implementation("dev.restate:sdk-testing:VAR::JAVA_SDK_VERSION") | ||
``` | ||
|
||
## Using the JUnit 5 Extension | ||
|
||
Given the service to test `GreeterService`, register the JUnit 5 extension that will start Restate along with the service: | ||
|
||
<Tabs groupId="sdk" queryString> | ||
<TabItem value="java" label="Java"> | ||
```java | ||
CODE_LOAD::java/src/main/java/develop/GreeterTest.java#extension | ||
``` | ||
</TabItem> | ||
<TabItem value="kotlin" label="Kotlin"> | ||
```kotlin | ||
CODE_LOAD::kotlin/src/main/kotlin/develop/GreeterTest.kt#extension | ||
``` | ||
</TabItem> | ||
</Tabs> | ||
|
||
Note that the extension will start one Restate server for the whole test class. For more details, checkout [`RestateRunner` Javadocs](/javadocs/dev/restate/sdk/testing/RestateRunner.html). | ||
|
||
Once the extension is set, you can implement your test methods as usual, and inject a [`Client`](/javadocs/dev/restate/sdk/client/Client.html) using [`@RestateClient`](/javadocs/dev/restate/sdk/testing/RestateClient.html) to interact with Restate and the registered services: | ||
|
||
<Tabs groupId="sdk" queryString> | ||
<TabItem value="java" label="Java"> | ||
```java | ||
CODE_LOAD::java/src/main/java/develop/GreeterTest.java#test | ||
``` | ||
</TabItem> | ||
<TabItem value="kotlin" label="Kotlin"> | ||
```kotlin | ||
CODE_LOAD::kotlin/src/main/kotlin/develop/GreeterTest.kt#test | ||
``` | ||
</TabItem> | ||
</Tabs> | ||
|
||
## Usage without JUnit 5 | ||
|
||
You can use the testing tools without JUnit 5 by creating a [`ManualRestateRunner`](/javadocs/dev/restate/sdk/testing/ManualRestateRunner.html) with [`RestateRunnerBuilder#buildManualRunner`](/javadocs/dev/restate/sdk/testing/RestateRunnerBuilder.html#buildManualRunner()). | ||
For more details, refer to the Javadocs. |