Skip to content

Commit

Permalink
docs: README.md update (#40)
Browse files Browse the repository at this point in the history
* docs: Updated README.md

Signed-off-by: Nahuel Rodriguez <12597182+Nahuel92@users.noreply.github.com>
  • Loading branch information
Nahuel92 authored Dec 1, 2024
1 parent 71738f8 commit e996718
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,40 @@
In your `pom.xml`, simply add the `wiremock-micronaut` dependency:

```xml

<dependency>
<groupId>io.github.nahuel92</groupId>
<artifactId>wiremock-micronaut</artifactId>
<version>1.8.4</version>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
```

## How to use

Use `@EnableWireMock` with `@ConfigureWireMock` with tests annotated that use `MicronautJunit5Extension`,
like `@MicronautTest`:
Use `@MicronautWireMockTest` with `@ConfigureWireMock`:

```java
@MicronautTest
@EnableWireMock(

@MicronautWireMockTest(
@ConfigureWireMock(
name = "user-service",
properties = "user-client.url"
)
)
@Property(name = "myProp", value = "myVal")
class TodoControllerTests {
@InjectWireMock("user-service")
private WireMockServer wiremock;

@Value("${user-client.url}")
private String wiremockUrl; // will contain the base URL for this WireMock instance.
private String wiremockUrlValue; // will contain the base URL for this WireMock instance.

@Property("user-client.url")
private String wiremockUrlProperty; // will contain the base URL for this WireMock instance.

@Test
void yourSUTTest() {
void yourSUTTest(@Property("myProp") final String myProp) {
// given
wiremock.stubFor(/*Your request*/);

Expand All @@ -55,12 +59,14 @@ class TodoControllerTests {
}
```

- `@EnableWireMock` adds test context customizer and enables `WireMockMicronautExtension`.
- `@MicronautWireMockTest` is an enhanced `@MicronautTest` annotation with WireMock capabilities, which means you can
pass the same parameters as you would with `@MicronautTest`.
- `@ConfigureWireMock` creates a `WireMockServer` and passes the `WireMockServer.baseUrl` to a Micronaut environment
property with a name given by a property.
- `@InjectWireMock` injects `WireMockServer` instances to your test.
property with a name given by a property
- `@InjectWireMock` injects `WireMockServer` instances to your test

> **Note:** `WireMockServer` instances aren't added as beans to the Micronaut application context. Instead, instances
> [!NOTE]
> `WireMockServer` instances aren't added as beans to the Micronaut application context. Instead, instances
> are kept in a separate store associated with the application context used by tests.
### Single Property Injection
Expand All @@ -70,8 +76,8 @@ exclusive `WireMockServer` instance. You get maximum isolation between your serv
complex test setup.

```java
@MicronautTest
@EnableWireMock({

@MicronautWireMockTest({
@ConfigureWireMock(
name = "foo-service",
properties = "app.client-apis.foo.base-path"
Expand Down Expand Up @@ -108,8 +114,8 @@ The following example shows how to use the *Multiple Property Injection*, which
`WireMockServer` instance. You give up on isolation between your services' mocks, but you get a less complex test setup.

```java
@MicronautTest
@EnableWireMock(

@MicronautWireMockTest(
@ConfigureWireMock(
name = "services",
properties = {
Expand All @@ -135,8 +141,8 @@ class YourTest {
Usually, you'll configure your tests as follows:

```java
@MicronautTest
@EnableWireMock({

@MicronautWireMockTest({
@ConfigureWireMock(
name = "todo-client",
properties = "todo-client.url",
Expand Down Expand Up @@ -169,8 +175,8 @@ class YourTest {
Or, if you need only one server:

```java
@MicronautTest
@EnableWireMock(

@MicronautWireMockTest(
@ConfigureWireMock(
name = "todo-client",
properties = "todo-client.url",
Expand All @@ -196,8 +202,8 @@ In the previous situation, when the test only requires exactly one WireMock serv
In this case, the `WireMock` client class can be used to configure your stubs:

```java
@MicronautTest
@EnableWireMock(

@MicronautWireMockTest(
@ConfigureWireMock(
name = "todo-client",
properties = "todo-client.url",
Expand All @@ -221,8 +227,8 @@ class YourTest {
By default, classpath location is used to get stubs:

```java
@MicronautTest
@EnableWireMock(

@MicronautWireMockTest(
@ConfigureWireMock(
name = "todo-client",
properties = "todo-client.url",
Expand All @@ -249,8 +255,8 @@ But sometimes you may want to use any directory on the file system.
To achieve that, you can override a property called `stubLocationOnClasspath` on the `@ConfigureWireMock`:

```java
@MicronautTest
@EnableWireMock(

@MicronautWireMockTest(
@ConfigureWireMock(
name = "todo-client",
properties = "todo-client.url",
Expand Down Expand Up @@ -289,8 +295,8 @@ In the following example, WireMock is instructed to:
- Search for service descriptor files under `src/test/resources/wiremock`.

```java
@MicronautTest
@EnableWireMock({

@MicronautWireMockTest({
@ConfigureWireMock(
name = GreeterGrpc.SERVICE_NAME,
portProperty = "my.port",
Expand Down Expand Up @@ -324,8 +330,8 @@ public class GrpcTest {
It also supports multiple gRPC and HTTP stubs at the same time, although you may want to stick to simpler tests:

```java
@MicronautTest
@EnableWireMock({

@MicronautWireMockTest({
@ConfigureWireMock(
name = GreeterGrpc.SERVICE_NAME,
portProperty = "my.port",
Expand Down

0 comments on commit e996718

Please sign in to comment.