Skip to content

Commit 30b1645

Browse files
committed
Include parameters
Signed-off-by: Jorge Bescos Gascon <[email protected]>
1 parent e0cd11b commit 30b1645

File tree

3 files changed

+100
-13
lines changed

3 files changed

+100
-13
lines changed

microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@
4545
import jakarta.enterprise.inject.se.SeContainer;
4646
import jakarta.enterprise.inject.se.SeContainerInitializer;
4747
import jakarta.enterprise.inject.spi.AfterBeanDiscovery;
48+
import jakarta.enterprise.inject.spi.AnnotatedParameter;
4849
import jakarta.enterprise.inject.spi.BeforeBeanDiscovery;
4950
import jakarta.enterprise.inject.spi.CDI;
5051
import jakarta.enterprise.inject.spi.Extension;
5152
import jakarta.enterprise.inject.spi.InjectionPoint;
5253
import jakarta.enterprise.inject.spi.ProcessAnnotatedType;
5354
import jakarta.enterprise.inject.spi.ProcessInjectionPoint;
5455
import jakarta.enterprise.inject.spi.WithAnnotations;
55-
import jakarta.enterprise.inject.spi.configurator.AnnotatedFieldConfigurator;
5656
import jakarta.enterprise.inject.spi.configurator.AnnotatedTypeConfigurator;
5757
import jakarta.enterprise.util.AnnotationLiteral;
5858
import jakarta.inject.Inject;
@@ -541,16 +541,29 @@ void processSocketInjectionPoints(@Observes ProcessInjectionPoint<?, WebTarget>
541541

542542
void processMockBean(@Observes @WithAnnotations(MockBean.class) ProcessAnnotatedType<?> obj) throws Exception {
543543
var configurator = obj.configureAnnotatedType();
544-
for (AnnotatedFieldConfigurator<?> field : configurator.fields()) {
544+
configurator.fields().forEach(field -> {
545545
MockBean mockBean = field.getAnnotated().getAnnotation(MockBean.class);
546546
if (mockBean != null) {
547547
Field f = field.getAnnotated().getJavaMember();
548-
// Adds @Inject if not found, so it is more user friendly
548+
// Adds @Inject to be more user friendly
549549
field.add(Literal.INSTANCE);
550550
Class<?> fieldType = f.getType();
551551
mocks.add(fieldType);
552552
}
553-
}
553+
});
554+
configurator.constructors().forEach(constructor -> {
555+
processMockBeanParameters(constructor.getAnnotated().getParameters());
556+
});
557+
}
558+
559+
private void processMockBeanParameters(List<? extends AnnotatedParameter<?>> parameters) {
560+
parameters.stream().forEach(parameter -> {
561+
MockBean mockBean = parameter.getAnnotation(MockBean.class);
562+
if (mockBean != null) {
563+
Class<?> parameterType = parameter.getJavaParameter().getType();
564+
mocks.add(parameterType);
565+
}
566+
});
554567
}
555568

556569
void registerOtherBeans(@Observes AfterBeanDiscovery event) {
+9-9
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
import org.mockito.Mockito;
3333

3434
@HelidonTest
35-
@AddBean(TestMockBean.Resource.class)
36-
@AddBean(TestMockBean.Service.class)
37-
@AddBean(TestMockBean.OtherService.class)
38-
public class TestMockBean {
35+
@AddBean(TestMockBeanField.Resource.class)
36+
@AddBean(TestMockBeanField.Service.class)
37+
@AddBean(TestMockBeanField.OtherService.class)
38+
class TestMockBeanField {
3939

4040
// Without @Inject
4141
@MockBean
@@ -48,7 +48,7 @@ public class TestMockBean {
4848
private WebTarget target;
4949

5050
@Test
51-
public void injectionTest() {
51+
void injectionTest() {
5252
Mockito.when(service.test()).thenReturn("Mocked");
5353
String response = target.path("/test").request().get(String.class);
5454
assertThat(response, is("Mocked"));
@@ -68,17 +68,17 @@ public String test() {
6868
}
6969
}
7070

71-
public static class Service {
71+
static class Service {
7272

73-
public String test() {
73+
String test() {
7474
return "Not Mocked";
7575
}
7676

7777
}
7878

79-
public static class OtherService {
79+
static class OtherService {
8080

81-
public String test() {
81+
String test() {
8282
return "OtherService";
8383
}
8484

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
17+
package io.helidon.microprofile.tests.testing.junit5;
18+
19+
import static org.hamcrest.CoreMatchers.is;
20+
import static org.hamcrest.MatcherAssert.assertThat;
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 io.helidon.microprofile.testing.junit5.AddBean;
28+
import io.helidon.microprofile.testing.junit5.HelidonTest;
29+
import io.helidon.microprofile.testing.junit5.MockBean;
30+
31+
import org.junit.jupiter.api.Test;
32+
import org.mockito.Mockito;
33+
34+
@HelidonTest
35+
@AddBean(TestMockBeanParameter.Resource.class)
36+
@AddBean(TestMockBeanParameter.Service.class)
37+
class TestMockBeanParameter {
38+
39+
private final Service service;
40+
private final WebTarget target;
41+
42+
@Inject
43+
TestMockBeanParameter(@MockBean Service service, WebTarget target) {
44+
this.service = service;
45+
this.target = target;
46+
}
47+
48+
@Test
49+
void injectionTest() {
50+
Mockito.when(service.test()).thenReturn("Mocked");
51+
String response = target.path("/test").request().get(String.class);
52+
assertThat(response, is("Mocked"));
53+
}
54+
55+
@Path("/test")
56+
public static class Resource {
57+
58+
@Inject
59+
private Service service;
60+
61+
@GET
62+
public String test() {
63+
return service.test();
64+
}
65+
}
66+
67+
static class Service {
68+
69+
String test() {
70+
return "Not Mocked";
71+
}
72+
73+
}
74+
}

0 commit comments

Comments
 (0)