-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* 4.x: Ability to Inject MockBeans in Helidon #7694 Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
- Loading branch information
Showing
19 changed files
with
609 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,6 @@ | |
<artifactId>hamcrest-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?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-mocking</artifactId> | ||
<name>Helidon Microprofile Testing Mocking</name> | ||
|
||
<description> | ||
Integration with Mocking to support tests with CDI injection | ||
</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>jakarta.enterprise</groupId> | ||
<artifactId>jakarta.enterprise.cdi-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
40 changes: 40 additions & 0 deletions
40
...ofile/testing/mocking/src/main/java/io/helidon/microprofile/testing/mocking/MockBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.mocking; | ||
|
||
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; | ||
} |
92 changes: 92 additions & 0 deletions
92
.../mocking/src/main/java/io/helidon/microprofile/testing/mocking/MockBeansCdiExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.mocking; | ||
|
||
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 Mocking 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); | ||
}); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...e/testing/mocking/src/main/java/io/helidon/microprofile/testing/mocking/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 mocking. | ||
*/ | ||
package io.helidon.microprofile.testing.mocking; |
31 changes: 31 additions & 0 deletions
31
microprofile/testing/mocking/src/main/java/module-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* Mock Beans extension module to run CDI tests. | ||
*/ | ||
module io.helidon.microprofile.testing.mocking { | ||
|
||
requires jakarta.inject; | ||
requires org.mockito; | ||
|
||
requires transitive jakarta.cdi; | ||
|
||
exports io.helidon.microprofile.testing.mocking; | ||
|
||
provides jakarta.enterprise.inject.spi.Extension | ||
with io.helidon.microprofile.testing.mocking.MockBeansCdiExtension; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,5 +35,6 @@ | |
<modules> | ||
<module>junit5</module> | ||
<module>testng</module> | ||
<module>mocking</module> | ||
</modules> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.