-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
- Loading branch information
Showing
20 changed files
with
196 additions
and
172 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
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 |
---|---|---|
@@ -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> | ||
<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> | ||
<artifactId>helidon-microprofile-cdi</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
</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
91 changes: 91 additions & 0 deletions
91
...-beans/src/main/java/io/helidon/microprofile/testing/mockbeans/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,91 @@ | ||
/* | ||
* 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.HashSet; | ||
import java.util.List; | ||
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 Set<Class<?>> mocks = new HashSet<>(); | ||
|
||
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.add(fieldType); | ||
} | ||
}); | ||
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.add(parameterType); | ||
} | ||
}); | ||
} | ||
|
||
void registerOtherBeans(@Observes AfterBeanDiscovery event, BeanManager beanManager) { | ||
// Register all mocks | ||
mocks.forEach(type -> { | ||
event.addBean() | ||
.addType(type) | ||
.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(type, mockSettings); | ||
} else { | ||
return Mockito.mock(type); | ||
} | ||
}) | ||
.priority(0); | ||
}); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ting/mock-beans/src/main/java/io/helidon/microprofile/testing/mockbeans/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 mock beans. | ||
*/ | ||
package io.helidon.microprofile.testing.mockbeans; |
Oops, something went wrong.