Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.x: Ability to Inject MockBeans in Helidon #7694 #8674

Merged
merged 13 commits into from
May 17, 2024
4 changes: 4 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,10 @@
<groupId>io.helidon.messaging.mock</groupId>
<artifactId>helidon-messaging-mock</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-mock-beans</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.logging</groupId>
<artifactId>helidon-logging-common</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,11 @@
<artifactId>helidon-messaging-mock</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-mock-beans</artifactId>
<version>${helidon.version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>io.helidon.logging</groupId>
Expand Down
6 changes: 6 additions & 0 deletions dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<version.lib.animal-sniffer>1.18</version.lib.animal-sniffer>
<version.lib.annotation-api>1.3.5</version.lib.annotation-api>
<version.lib.brave-opentracing>1.0.0</version.lib.brave-opentracing>
<version.lib.bytebuddy>1.14.14</version.lib.bytebuddy>
<version.lib.commons-codec>1.16.0</version.lib.commons-codec>
<version.lib.commons-logging>1.2</version.lib.commons-logging>
<version.lib.cron-utils>9.1.6</version.lib.cron-utils>
Expand Down Expand Up @@ -501,6 +502,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>${version.lib.bytebuddy}</version>
</dependency>
barchetta marked this conversation as resolved.
Show resolved Hide resolved
<dependency>
<groupId>jakarta.websocket</groupId>
<artifactId>jakarta.websocket-api</artifactId>
Expand Down
1 change: 0 additions & 1 deletion microprofile/testing/junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
2 changes: 1 addition & 1 deletion microprofile/testing/junit5/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
* Copyright (c) 2020, 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.
Expand Down
47 changes: 47 additions & 0 deletions microprofile/testing/mock-beans/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-project</artifactId>
<version>4.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>helidon-microprofile-testing-mock-beans</artifactId>
romain-grecourt marked this conversation as resolved.
Show resolved Hide resolved
<name>Helidon Microprofile Testing Mock Beans</name>

<description>
Integration with Mock Beans to support tests with CDI injection
</description>

<dependencies>
<dependency>
<groupId>io.helidon.microprofile.cdi</groupId>
tomas-langer marked this conversation as resolved.
Show resolved Hide resolved
<artifactId>helidon-microprofile-cdi</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
romain-grecourt marked this conversation as resolved.
Show resolved Hide resolved
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.testing.mockbeans;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.mockito.Answers;

/**
* A field annotated with @MockBean will be mocked by Mockito
* and injected in every place it is referenced.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
public @interface MockBean {

/**
* The {@link Answers} type to use on the mock.
* Defaults to {@link Answers#RETURNS_DEFAULTS}
* @return the answer type
*/
Answers answer() default Answers.RETURNS_DEFAULTS;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.testing.mockbeans;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.literal.InjectLiteral;
import jakarta.enterprise.inject.spi.AfterBeanDiscovery;
import jakarta.enterprise.inject.spi.AnnotatedParameter;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.Extension;
import jakarta.enterprise.inject.spi.ProcessAnnotatedType;
import jakarta.enterprise.inject.spi.WithAnnotations;
import org.mockito.MockSettings;
import org.mockito.Mockito;

/**
* CDI extension for Mock Beans implementation.
*/
public class MockBeansCdiExtension implements Extension {

private final Map<Class<?>, MockBean> mocks = new HashMap<>();

void processMockBean(@Observes @WithAnnotations(MockBean.class) ProcessAnnotatedType<?> obj) throws Exception {
var configurator = obj.configureAnnotatedType();
configurator.fields().forEach(field -> {
MockBean mockBean = field.getAnnotated().getAnnotation(MockBean.class);
if (mockBean != null) {
Field f = field.getAnnotated().getJavaMember();
// Adds @Inject to be more user friendly
field.add(InjectLiteral.INSTANCE);
Class<?> fieldType = f.getType();
mocks.put(fieldType, mockBean);
}
});
configurator.constructors().forEach(constructor -> {
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.put(parameterType, mockBean);
}
});
}

void registerOtherBeans(@Observes AfterBeanDiscovery event, BeanManager beanManager) {
// Register all mocks
mocks.entrySet().forEach(entry -> {
event.addBean()
.addType(entry.getKey())
.scope(ApplicationScoped.class)
.alternative(true)
.createWith(inst -> {
Set<Bean<?>> beans = beanManager.getBeans(MockSettings.class);
if (!beans.isEmpty()) {
Bean<?> bean = beans.iterator().next();
MockSettings mockSettings = (MockSettings) beanManager.getReference(bean, MockSettings.class,
beanManager.createCreationalContext(null));
return Mockito.mock(entry.getKey(), mockSettings);
} else {
return Mockito.mock(entry.getKey(), Mockito.withSettings().defaultAnswer(entry.getValue().answer()));
}
})
.priority(0);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/

/**
* Helidon implementation of mock beans.
*/
package io.helidon.microprofile.testing.mockbeans;
32 changes: 32 additions & 0 deletions microprofile/testing/mock-beans/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.
*/

import io.helidon.microprofile.testing.mockbeans.MockBeansCdiExtension;

/**
* Mock Beans extension module to run CDI tests.
*/
module io.helidon.microprofile.testing.mockbeans {

requires jakarta.inject;
requires org.mockito;

requires transitive jakarta.cdi;

exports io.helidon.microprofile.testing.mockbeans;

provides jakarta.enterprise.inject.spi.Extension with MockBeansCdiExtension;
romain-grecourt marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions microprofile/testing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
<modules>
<module>junit5</module>
<module>testng</module>
<module>mock-beans</module>
</modules>
</project>
1 change: 0 additions & 1 deletion microprofile/testing/testng/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml-mp</artifactId>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion microprofile/testing/testng/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 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.
Expand Down
5 changes: 5 additions & 0 deletions microprofile/tests/testing/junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-mock-beans</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Loading