From 06aa3d08cafa9a63c770091f5815432f2b431071 Mon Sep 17 00:00:00 2001 From: Thomas Mortagne Date: Tue, 16 Jul 2024 11:18:53 +0200 Subject: [PATCH] [Misc] Improve environment handling during tests * make sure to always use a test environment for extension handler tests --- .../extension/test/AbstractExtensionHandlerTest.java | 9 +++++++-- .../org/xwiki/extension/test/MockitoRepositoryUtils.java | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-api/src/test/java/org/xwiki/extension/test/AbstractExtensionHandlerTest.java b/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-api/src/test/java/org/xwiki/extension/test/AbstractExtensionHandlerTest.java index 0310919d68..394d7bee17 100644 --- a/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-api/src/test/java/org/xwiki/extension/test/AbstractExtensionHandlerTest.java +++ b/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-api/src/test/java/org/xwiki/extension/test/AbstractExtensionHandlerTest.java @@ -28,6 +28,7 @@ import org.xwiki.cache.CacheFactory; import org.xwiki.cache.CacheManager; import org.xwiki.configuration.internal.MemoryConfigurationSource; +import org.xwiki.environment.Environment; import org.xwiki.extension.ExtensionId; import org.xwiki.extension.InstalledExtension; import org.xwiki.extension.LocalExtension; @@ -44,6 +45,7 @@ import org.xwiki.job.Request; import org.xwiki.logging.LogLevel; import org.xwiki.logging.event.LogEvent; +import org.xwiki.test.TestEnvironment; import org.xwiki.test.annotation.AfterComponent; import org.xwiki.test.annotation.AllComponents; import org.xwiki.test.junit5.mockito.InjectComponentManager; @@ -67,11 +69,14 @@ public abstract class AbstractExtensionHandlerTest protected MemoryConfigurationSource memoryConfigurationSource; - // We inject infinispan after components have been loaded to be sure to mock it only if - // it's not available in all components. @AfterComponent public void afterComponent() throws Exception { + // Force a test environment + this.componentManager.registerComponent(Environment.class, new TestEnvironment()); + + // We inject infinispan after components have been loaded to be sure to mock it only if + // it's not available in all components. if (!this.componentManager.hasComponent(CacheFactory.class, "infinispan")) { CacheFactory infinispan = this.componentManager.registerMockComponent(CacheFactory.class, "infinispan"); when(infinispan.newCache(any())).thenReturn(mock(Cache.class)); diff --git a/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-api/src/test/java/org/xwiki/extension/test/MockitoRepositoryUtils.java b/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-api/src/test/java/org/xwiki/extension/test/MockitoRepositoryUtils.java index 65b0d5e67c..ea8e6ee133 100644 --- a/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-api/src/test/java/org/xwiki/extension/test/MockitoRepositoryUtils.java +++ b/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-api/src/test/java/org/xwiki/extension/test/MockitoRepositoryUtils.java @@ -49,6 +49,7 @@ public MockitoRepositoryUtils(MockitoComponentManager componentManager) this.componentManager = componentManager; } + @Override public void setup() throws Exception { // If an Environment is already configured, reuse it