Skip to content

Commit 6ac5fd5

Browse files
committed
Helidon mock documentation
Signed-off-by: tvallin <[email protected]>
1 parent ab3ee29 commit 6ac5fd5

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

docs/src/main/asciidoc/mp/testing/testing.adoc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,41 @@ include::{sourcedir}/mp/testing/TestingSnippets.java[tag=snippet_2, indent=0]
161161
162162
== Mock Support
163163
164+
This section describes how to mock objects using Helidon API and in a second phase, using pure CDI.
165+
166+
=== Helidon Mock Support
167+
168+
Helidon has its own API to use mocking with test classes annotated with `@HelidonTest`.
169+
170+
==== Maven Coordinates
171+
172+
To enable Helidon Mock Support add the following dependency to your project’s pom.xml.
173+
[source,xml]
174+
----
175+
<dependency>
176+
<groupId>io.helidon.microprofile.testing</groupId>
177+
<artifactId>helidon-microprofile-testing-mocking</artifactId>
178+
<scope>test</scope>
179+
</dependency>
180+
----
181+
182+
==== API
183+
184+
It consists of one annotation named `@MockBean`, designed to be used on fields and parameters. The implementation
185+
relies only on CDI and thus it works with either JUnit or TestNG. The annotation has a parameter `answers` used
186+
to set the default answer for the mocked beans.
187+
188+
==== Example
189+
190+
[source,java]
191+
.Code sample
192+
----
193+
include::{sourcedir}/mp/testing/HelidonMockingSnippets.java[tag=snippet_1, indent=0]
194+
----
195+
<1> `service` field annotated with `@MockBean` and `Answers.CALLS_REAL_METHODS` for default answers.
196+
<2> Test the mocked service with real method response.
197+
<3> Test the mocked service with modified behavior.
198+
164199
=== Mocking objects with pure CDI
165200
166201
CDI can be used to enable mocking, the following example shows how to mock a service and inject it in a JAX-RS resource.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2024 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.docs.mp.testing;
17+
18+
import io.helidon.microprofile.testing.junit5.AddBean;
19+
import io.helidon.microprofile.testing.junit5.HelidonTest;
20+
import io.helidon.microprofile.testing.mocking.MockBean;
21+
22+
import jakarta.inject.Inject;
23+
import jakarta.ws.rs.GET;
24+
import jakarta.ws.rs.Path;
25+
import jakarta.ws.rs.client.WebTarget;
26+
27+
import org.junit.jupiter.api.Test;
28+
import org.mockito.Answers;
29+
import org.mockito.Mockito;
30+
31+
import static org.hamcrest.MatcherAssert.assertThat;
32+
import static org.hamcrest.Matchers.is;
33+
34+
@SuppressWarnings("ALL")
35+
class HelidonMockingSnippets {
36+
37+
// tag::snippet_1[]
38+
@HelidonTest
39+
@AddBean(MockBeanAnswerTest.Resource.class)
40+
@AddBean(MockBeanAnswerTest.Service.class)
41+
class MockBeanAnswerTest {
42+
43+
@MockBean(answer = Answers.CALLS_REAL_METHODS) // <1>
44+
private Service service;
45+
@Inject
46+
private WebTarget target;
47+
48+
@Test
49+
void injectionTest() {
50+
String response = target.path("/test").request().get(String.class);
51+
assertThat(response, is("Not Mocked")); // <2>
52+
Mockito.when(service.test()).thenReturn("Mocked");
53+
response = target.path("/test").request().get(String.class);
54+
assertThat(response, is("Mocked")); // <3>
55+
}
56+
57+
@Path("/test")
58+
public static class Resource {
59+
60+
@Inject
61+
private Service service;
62+
63+
@GET
64+
public String test() {
65+
return service.test();
66+
}
67+
}
68+
69+
static class Service {
70+
71+
String test() {
72+
return "Not Mocked";
73+
}
74+
75+
}
76+
}
77+
// end::snippet_1[]
78+
}

0 commit comments

Comments
 (0)