From 79a868b8fe4aa3da46916c4bafc0ede2101adfa5 Mon Sep 17 00:00:00 2001
From: LoveIsGrief <loveisgrief@tuta.io>
Date: Sat, 23 Jan 2021 14:17:27 +0000
Subject: [PATCH] Use TestContext to replace existing I2PAppContext

This allows us to mock parts of the I2PAppContext as we like.
The mocking is done when `testContext` is created in the constructor,
 which replaces the existing global context.
---
 core/java/test/junit/net/i2p/TestContext.java | 17 +++++++
 .../net/i2p/util/ConvertToHashMockTest.java   | 51 +++++++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 core/java/test/junit/net/i2p/TestContext.java
 create mode 100644 core/java/test/junit/net/i2p/util/ConvertToHashMockTest.java

diff --git a/core/java/test/junit/net/i2p/TestContext.java b/core/java/test/junit/net/i2p/TestContext.java
new file mode 100644
index 0000000000..a81bd40fb5
--- /dev/null
+++ b/core/java/test/junit/net/i2p/TestContext.java
@@ -0,0 +1,17 @@
+package net.i2p;
+
+public class TestContext extends I2PAppContext {
+
+    public TestContext() {
+        TestContext.setGlobalContext(this);
+    }
+
+    /**
+     * Allows overriding the existing I2PAppContext with a test context who's fields we may mock as we like
+     *
+     * @param ctx Our test context to replace the global context with
+     */
+    public static void setGlobalContext(TestContext ctx){
+        _globalAppContext = ctx;
+    }
+}
diff --git a/core/java/test/junit/net/i2p/util/ConvertToHashMockTest.java b/core/java/test/junit/net/i2p/util/ConvertToHashMockTest.java
new file mode 100644
index 0000000000..de811acd1c
--- /dev/null
+++ b/core/java/test/junit/net/i2p/util/ConvertToHashMockTest.java
@@ -0,0 +1,51 @@
+package net.i2p.util;
+
+import net.i2p.TestContext;
+import net.i2p.client.naming.NamingService;
+import net.i2p.data.Destination;
+import net.i2p.data.Hash;
+import org.junit.AfterClass;
+import org.junit.Before;
+
+import org.junit.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import static org.mockito.Mockito.*;
+import static org.junit.Assert.*;
+
+public class ConvertToHashMockTest{
+
+    @Mock private NamingService namingService;
+    @Mock private Destination destination;
+    @Mock private Hash hash;
+
+    @InjectMocks TestContext testContext;
+
+    @Before
+    public void before() {
+        MockitoAnnotations.initMocks(this);
+    }
+
+    /**
+     * Reset the global context after all tests in the class are done.
+     *
+     * We would otherwise pollute the other tests that depend on I2PAppContext
+     */
+    @AfterClass
+    public static void afterClass(){
+        TestContext.setGlobalContext(null);
+    }
+
+    @Test
+    public void testMockedDestination() {
+        when(namingService.lookup("zzz.i2p")).thenReturn(destination);
+        when(destination.calculateHash()).thenReturn(hash);
+
+        assertSame(hash, ConvertToHash.getHash("zzz.i2p"));
+
+        verify(namingService).lookup("zzz.i2p");
+        verify(destination).calculateHash();
+    }
+}
-- 
GitLab