diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java index 74f83179e01..05b58716020 100644 --- a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java +++ b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java @@ -45,6 +45,7 @@ import jakarta.enterprise.inject.se.SeContainer; import jakarta.enterprise.inject.se.SeContainerInitializer; import jakarta.enterprise.inject.spi.AfterBeanDiscovery; +import jakarta.enterprise.inject.spi.AnnotatedParameter; import jakarta.enterprise.inject.spi.BeforeBeanDiscovery; import jakarta.enterprise.inject.spi.CDI; import jakarta.enterprise.inject.spi.Extension; @@ -52,7 +53,6 @@ import jakarta.enterprise.inject.spi.ProcessAnnotatedType; import jakarta.enterprise.inject.spi.ProcessInjectionPoint; import jakarta.enterprise.inject.spi.WithAnnotations; -import jakarta.enterprise.inject.spi.configurator.AnnotatedFieldConfigurator; import jakarta.enterprise.inject.spi.configurator.AnnotatedTypeConfigurator; import jakarta.enterprise.util.AnnotationLiteral; import jakarta.inject.Inject; @@ -541,16 +541,31 @@ void processSocketInjectionPoints(@Observes ProcessInjectionPoint void processMockBean(@Observes @WithAnnotations(MockBean.class) ProcessAnnotatedType obj) throws Exception { var configurator = obj.configureAnnotatedType(); - for (AnnotatedFieldConfigurator field : configurator.fields()) { + configurator.fields().forEach(field -> { MockBean mockBean = field.getAnnotated().getAnnotation(MockBean.class); if (mockBean != null) { Field f = field.getAnnotated().getJavaMember(); - // Adds @Inject if not found, so it is more user friendly + // Adds @Inject to be more user friendly field.add(Literal.INSTANCE); Class fieldType = f.getType(); mocks.add(fieldType); } - } + }); + configurator.constructors().forEach(constructor -> { + // Adds @Inject to be more user friendly + constructor.add(Literal.INSTANCE); + processMockBeanParameters(constructor.getAnnotated().getParameters()); + }); + } + + private void processMockBeanParameters(List> parameters) { + parameters.stream().forEach(parameter -> { + MockBean mockBean = parameter.getAnnotation(MockBean.class); + if (mockBean != null) { + Class parameterType = parameter.getJavaParameter().getType(); + mocks.add(parameterType); + } + }); } void registerOtherBeans(@Observes AfterBeanDiscovery event) { diff --git a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestMockBean.java b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestMockBeanField.java similarity index 86% rename from microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestMockBean.java rename to microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestMockBeanField.java index 18a6fdba4f2..015849938b6 100644 --- a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestMockBean.java +++ b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestMockBeanField.java @@ -32,10 +32,10 @@ import org.mockito.Mockito; @HelidonTest -@AddBean(TestMockBean.Resource.class) -@AddBean(TestMockBean.Service.class) -@AddBean(TestMockBean.OtherService.class) -public class TestMockBean { +@AddBean(TestMockBeanField.Resource.class) +@AddBean(TestMockBeanField.Service.class) +@AddBean(TestMockBeanField.OtherService.class) +class TestMockBeanField { // Without @Inject @MockBean @@ -48,7 +48,7 @@ public class TestMockBean { private WebTarget target; @Test - public void injectionTest() { + void injectionTest() { Mockito.when(service.test()).thenReturn("Mocked"); String response = target.path("/test").request().get(String.class); assertThat(response, is("Mocked")); @@ -68,17 +68,17 @@ public String test() { } } - public static class Service { + static class Service { - public String test() { + String test() { return "Not Mocked"; } } - public static class OtherService { + static class OtherService { - public String test() { + String test() { return "OtherService"; } diff --git a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestMockBeanParameter.java b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestMockBeanParameter.java new file mode 100644 index 00000000000..95fd0a1fa11 --- /dev/null +++ b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestMockBeanParameter.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.microprofile.tests.testing.junit5; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.client.WebTarget; + +import io.helidon.microprofile.testing.junit5.AddBean; +import io.helidon.microprofile.testing.junit5.HelidonTest; +import io.helidon.microprofile.testing.junit5.MockBean; + +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +@HelidonTest +@AddBean(TestMockBeanParameter.Resource.class) +@AddBean(TestMockBeanParameter.Service.class) +class TestMockBeanParameter { + + private final Service service; + private final WebTarget target; + + TestMockBeanParameter(@MockBean Service service, WebTarget target) { + this.service = service; + this.target = target; + } + + @Test + void injectionTest() { + Mockito.when(service.test()).thenReturn("Mocked"); + String response = target.path("/test").request().get(String.class); + assertThat(response, is("Mocked")); + } + + @Path("/test") + public static class Resource { + + @Inject + private Service service; + + @GET + public String test() { + return service.test(); + } + } + + static class Service { + + String test() { + return "Not Mocked"; + } + + } +}