Skip to content

Commit

Permalink
Include parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos committed Apr 23, 2024
1 parent e0cd11b commit f28da29
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@
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;
import jakarta.enterprise.inject.spi.InjectionPoint;
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;
Expand Down Expand Up @@ -541,16 +541,31 @@ void processSocketInjectionPoints(@Observes ProcessInjectionPoint<?, WebTarget>

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<? extends AnnotatedParameter<?>> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"));
Expand All @@ -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";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}

}
}

0 comments on commit f28da29

Please sign in to comment.