Skip to content

Commit

Permalink
[Misc] Improve environment handling during tests
Browse files Browse the repository at this point in the history
* make sure to always use a test environment for extension handler tests
  • Loading branch information
tmortagne committed Jul 16, 2024
1 parent c04eafa commit 06aa3d0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 06aa3d0

Please sign in to comment.