You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Instead, we need to use CDI to provide an alternative. This solution works with Helidon 2.x, 3.x and 4.x.
@HelidonTest@Priority(1) // required for @AlternativeclassFooTest {
@InjectprivateWebTargettarget;
privateFooServicefooService;
@BeforeEachvoidinitMock() {
// set fooService to a new mock before each test// use real methods for default answersfooService = Mockito.mock(FooService.class, InvocationOnMock::callRealMethod);
}
@Produces@AlternativeFooServicemockFooService() {
// This makes FooResource inject our mock instead of the default singletonreturnfooService;
}
@TestvoidtestMockedFoo() {
// test the modified behaviorwhen(fooService.getFoo()).thenReturn("bar");
Responseresponse = target.path("/foo").request().get();
assertThat(response.getStatus(), is(200));
assertThat(response.readEntity(String.class), is("bar"));
}
@TestvoidtestFoo() {
// test the non modified behaviorResponseresponse = target.path("/foo").request().get();
assertThat(response.getStatus(), is(200));
assertThat(response.readEntity(String.class), is("foo"));
}
}
document how to setup mocks using CDI producer methods (I.e. the example above)
Investigate if there is a way to make HelidonTest and MockitoExtension work together.
Investigate if we can provide an annotation to setup a mock as an alternative
Investigate if we should support other use-cases (e.g. @org.mockito.Spy, @org.mockito.Captor)
Example of a custom annotation to polish the example above:
@HelidonTestclassFooTest {
// Creates a new mock instance for each test// and expose it as an alternative@io.helidon.microprofile.testing.junit5.MockprivateFooServicefooService;
@TestvoidtestMockedFoo() {
// same as above
}
}
The text was updated successfully, but these errors were encountered:
I would do it in the Spring way, it will be more intuitive for users (this is what you said in FooTest). There is an issue for this: #7694
We can declare a field annotated with @MockBean and that will be mocked by Mockito and set it in CDI container, so every dependency to that will contain the mock instance.
@jbescos This issue is about documenting what's currently doable (as of 4.0.7) ; the addition of a new new annotation to support mocked beans should be done in a separate issue.
@jbescos This issue is about documenting what's currently doable (as of 4.0.7) ; the addition of a new new annotation to support mocked beans should be done in a separate issue.
OMG, I assigned to myself a documentation issue :(
Environment Details
Problem Description
Mockito's
@Mock
does not work with@HelidonTest
.Consider the following JAXRS resource and CDI bean:
A test that combines both
@HelidonTest
and@Mock
does not work:Current solution
Instead, we need to use CDI to provide an alternative. This solution works with Helidon 2.x, 3.x and 4.x.
pom.xml:
Action items:
HelidonTest
andMockitoExtension
work together.@org.mockito.Spy
,@org.mockito.Captor
)Example of a custom annotation to polish the example above:
The text was updated successfully, but these errors were encountered: