This is an automated email from the ASF dual-hosted git repository.
rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwebbeans-meecrowave.git
The following commit(s) were added to refs/heads/master by this push:
new 8a596d2 [MEECROWAVE-276] fix meecrowave-junit for @MeecrowaveConfig
case
8a596d2 is described below
commit 8a596d21f7d6786b9149b10b96ca76576da4d63e
Author: Romain Manni-Bucau <[email protected]>
AuthorDate: Tue Jan 12 14:40:27 2021 +0100
[MEECROWAVE-276] fix meecrowave-junit for @MeecrowaveConfig case
---
.../src/main/jbake/content/testing/index.adoc | 6 ++
.../meecrowave/junit5/MeecrowaveExtension.java | 111 +++++++++++----------
.../junit5/MeecrowaveTestLifecycleTest.java | 3 +
pom.xml | 2 +-
4 files changed, 68 insertions(+), 54 deletions(-)
diff --git a/meecrowave-doc/src/main/jbake/content/testing/index.adoc
b/meecrowave-doc/src/main/jbake/content/testing/index.adoc
index b112ae1..b229045 100644
--- a/meecrowave-doc/src/main/jbake/content/testing/index.adoc
+++ b/meecrowave-doc/src/main/jbake/content/testing/index.adoc
@@ -83,6 +83,7 @@ which is close to `MonoMeecrowave.Runner` in term of usage.
[source,java]
----
@MeecrowaveConfig /*(some config)*/
+@TestInstance(PER_CLASS)
public class MeecrowaveConfigTest {
@ConfigurationInject
private Meecrowave.Builder config;
@@ -109,6 +110,11 @@ TIP: JUnit 5 integration provides an
`@AfterFirstInjection` method and `@AfterLa
which can be used to setup/reset some environment using injections once for a
set of test methods.
The methods must not have any parameter.
+IMPORTANT: when not using `@TestInstance(PER_CLASS)`, container is started per
test method. Generally speaking you should try to align the scope of your
container to the scope of validity of your beans.
+For a library it is generally the class (so `@MeecrowaveConfig
@TestInstance(PER_CLASS)`) and for an application the whole test set (so
`@MonoMeecrowaveConfig`).
+Note that using an `Extension` you can adjust mocks or spy beans dynamically
without a container restart.
+Having the longest life time for the container will make your test suite
faster to execute.
+
== Arquillian Container
Container dependency:
diff --git
a/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveExtension.java
b/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveExtension.java
index 18aaeea..2464188 100644
---
a/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveExtension.java
+++
b/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveExtension.java
@@ -18,17 +18,6 @@
*/
package org.apache.meecrowave.junit5;
-import static java.util.Optional.ofNullable;
-import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation;
-
-import java.io.File;
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.util.Optional;
-
-import javax.enterprise.context.spi.CreationalContext;
-
import org.apache.meecrowave.Meecrowave;
import org.apache.meecrowave.configuration.Configuration;
import org.apache.meecrowave.testing.Injector;
@@ -38,32 +27,68 @@ import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
+import javax.enterprise.context.spi.CreationalContext;
+import java.io.File;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Optional;
+
+import static java.util.Optional.ofNullable;
+import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation;
+
public class MeecrowaveExtension extends BaseLifecycle
implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback,
AfterEachCallback {
-
- private static final ExtensionContext.Namespace NAMESPACE =
ExtensionContext.Namespace.create(MeecrowaveExtension.class.getName());
-
+
+ private static final ExtensionContext.Namespace NAMESPACE =
ExtensionContext.Namespace.create(MeecrowaveExtension.class.getName());
+
private final ScopesExtension scopes = new ScopesExtension() {
@Override
protected Optional<Class<? extends Annotation>[]> getScopes(final
ExtensionContext context) {
return context.getElement()
- .map(e -> findAnnotation(context.getElement(),
MeecrowaveConfig.class)
- .orElseGet(() -> context.getParent()
-
.flatMap(ExtensionContext::getElement)
- .flatMap(it ->
findAnnotation(it, MeecrowaveConfig.class))
- .orElse(null)))
- .map(MeecrowaveConfig::scopes)
- .filter(s -> s.length > 0);
+ .map(e -> findConfig(context))
+ .map(MeecrowaveConfig::scopes)
+ .filter(s -> s.length > 0);
}
};
@Override
public void beforeAll(final ExtensionContext context) {
+ if (isPerClass(context)) {
+ doStart(context);
+ }
+ }
+
+ @Override
+ public void afterAll(final ExtensionContext context) {
+ final ExtensionContext.Store store = context.getStore(NAMESPACE);
+ ofNullable(store.get(LifecyleState.class, LifecyleState.class))
+ .ifPresent(s -> s.afterLastTest(context));
+ if (isPerClass(context)) {
+ store.get(Meecrowave.class, Meecrowave.class).close();
+ }
+ }
+
+ @Override
+ public void beforeEach(final ExtensionContext context) {
+ if (!isPerClass(context)) {
+ doStart(context);
+ }
+ }
+
+ @Override
+ public void afterEach(final ExtensionContext context) {
+ if (!isPerClass(context)) {
+ doRelease(context);
+ context.getStore(NAMESPACE).get(Meecrowave.class,
Meecrowave.class).close();
+ }
+ }
+
+ private void doStart(final ExtensionContext context) {
final Meecrowave.Builder builder = new Meecrowave.Builder();
- final Optional<MeecrowaveConfig> meecrowaveConfig =
findAnnotation(context.getElement(), MeecrowaveConfig.class);
+ final MeecrowaveConfig config = findConfig(context);
final String ctx;
- if (meecrowaveConfig.isPresent()) {
- final MeecrowaveConfig config = meecrowaveConfig.get();
+ if (config != null) {
ctx = config.context();
for (final Method method : MeecrowaveConfig.class.getMethods()) {
@@ -104,39 +129,19 @@ public class MeecrowaveExtension extends BaseLifecycle
store.put(Meecrowave.Builder.class, builder);
meecrowave.bake(ctx);
- if (isPerClass(context)) {
- doInject(context);
- store.put(LifecyleState.class, onInjection(context, null));
- }
- }
-
- @Override
- public void afterAll(final ExtensionContext context) {
- final ExtensionContext.Store store = context.getStore(NAMESPACE);
- ofNullable(store.get(LifecyleState.class, LifecyleState.class))
- .ifPresent(s -> s.afterLastTest(context));
- if (isPerClass(context)) {
- store.get(Meecrowave.class, Meecrowave.class).close();
- }
- }
-
- @Override
- public void beforeEach(final ExtensionContext context) {
- if (!isPerClass(context)) {
- doInject(context);
- final ExtensionContext.Store store =
context.getParent().orElse(context).getStore(NAMESPACE);
- store.put(LifecyleState.class, onInjection(context,
store.get(LifecyleState.class, LifecyleState.class)));
- }
+ doInject(context);
+ store.put(LifecyleState.class, onInjection(context, null));
}
- @Override
- public void afterEach(final ExtensionContext context) {
- if (!isPerClass(context)) {
- doRelease(context);
- }
+ private MeecrowaveConfig findConfig(final ExtensionContext context) {
+ return findAnnotation(context.getElement(), MeecrowaveConfig.class)
+ .orElseGet(() -> context.getParent()
+ .flatMap(ExtensionContext::getElement)
+ .flatMap(it -> findAnnotation(it,
MeecrowaveConfig.class))
+ .orElse(null));
}
- private void doRelease(final ExtensionContext context) {
+ private void doRelease(final ExtensionContext context) {
ofNullable(context.getStore(NAMESPACE).get(CreationalContext.class,
CreationalContext.class))
.ifPresent(CreationalContext::release);
scopes.beforeEach(context);
diff --git
a/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveTestLifecycleTest.java
b/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveTestLifecycleTest.java
index b2b2e0a..85e2e4f 100644
---
a/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveTestLifecycleTest.java
+++
b/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveTestLifecycleTest.java
@@ -21,6 +21,7 @@ package org.apache.meecrowave.junit5;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
import javax.inject.Inject;
@@ -29,7 +30,9 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+@TestInstance(PER_CLASS)
@MeecrowaveConfig(scanningPackageIncludes =
"org.apache.meecrowave.junit5.bean")
class MeecrowaveTestLifecycleTest {
private static Appender global;
diff --git a/pom.xml b/pom.xml
index ac6b095..7dbc148 100644
--- a/pom.xml
+++ b/pom.xml
@@ -165,7 +165,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
- <version>3.0.0-M3</version>
+ <version>3.0.0-M5</version>
<configuration>
<trimStackTrace>false</trimStackTrace>
</configuration>