✅ Mock methods instead of setting private fields
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 28 Sep 2024 16:04:37 +0000 (18:04 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 28 Sep 2024 16:04:37 +0000 (18:04 +0200)
src/main/java/net/pterodactylus/sone/core/FreenetInterface.java
src/main/kotlin/net/pterodactylus/sone/main/FreenetModule.kt
src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt
src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt
src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt

index 0f05fc7..32f97b2 100644 (file)
@@ -223,7 +223,7 @@ public class FreenetInterface {
         */
        public void insertImage(TemporaryImage temporaryImage, Image image, InsertToken insertToken) throws SoneException {
                String filenameHint = image.getId() + "." + temporaryImage.getMimeType().substring(temporaryImage.getMimeType().lastIndexOf("/") + 1);
-               InsertableClientSSK key = InsertableClientSSK.createRandom(node.random, "");
+               InsertableClientSSK key = InsertableClientSSK.createRandom(node.getRandom(), "");
                FreenetURI targetUri = key.getInsertURI().setDocName(filenameHint);
                InsertContext insertContext = client.getInsertContext(true);
                RandomAccessBucket bucket = new ArrayBucket(temporaryImage.getImageData());
index 691ae4e..f2ff6fc 100644 (file)
@@ -33,7 +33,7 @@ class FreenetModule(private val pluginRespirator: PluginRespirator) : Module {
 
        @Provides
        fun getNodeClientCore(node: Node): NodeClientCore =
-               node.clientCore
+               node.getClientCore()
 
        @Provides
        fun getHighLevelSimpleClientCreator(nodeClientCore: NodeClientCore): HighLevelSimpleClientCreator =
@@ -41,10 +41,10 @@ class FreenetModule(private val pluginRespirator: PluginRespirator) : Module {
 
        @Provides
        fun getClientContext(nodeClientCore: NodeClientCore): ClientContext =
-               nodeClientCore.clientContext
+               nodeClientCore.getClientContext()
 
        @Provides
        fun getUskManager(nodeClientCore: NodeClientCore): USKManager =
-               nodeClientCore.uskManager
+               nodeClientCore.getUskManager()
 
 }
index ffd5c60..ca5e21b 100644 (file)
@@ -69,7 +69,7 @@ class FreenetInterfaceTest {
        private val freenetInterface: FreenetInterface
 
        init {
-               setField(node, "random", randomSource)
+               whenever(node.getRandom()).thenReturn(randomSource)
                freenetInterface = FreenetInterface(eventBus, node, uskManager, clientContext, soneUriCreator) { _, _, _ -> highLevelSimpleClient }
                insertToken = freenetInterface.InsertToken(image)
                insertToken.setBucket(bucket)
index 84cb567..476b935 100644 (file)
@@ -33,11 +33,11 @@ class FreenetModuleTest {
                whenever(getSessionManager("Sone")).thenReturn(sessionManager)
        }
        private val nodeClientCore = mock<NodeClientCore>().also {
-               setField(it, "uskManager", uskManager)
-               setField(it, "clientContext", clientContext)
+               whenever(it.getUskManager()).thenReturn(uskManager)
+               whenever(it.getClientContext()).thenReturn(clientContext)
        }
        private val node = pluginRespirator.node!!.also {
-               setField(it, "clientCore", nodeClientCore)
+               whenever(it.getClientCore()).thenReturn(nodeClientCore)
        }
        private val highLevelSimpleClient = pluginRespirator.hlSimpleClient!!
        private val toadletContainer: ToadletContainer = pluginRespirator.toadletContainer
index 3983aa2..dfa4830 100644 (file)
@@ -35,10 +35,10 @@ class SonePluginTest {
        private val uskManager = deepMock<USKManager>()
 
        init {
-               setField(node, "clientCore", clientCore)
+               whenever(node.getClientCore()).thenReturn(clientCore)
                whenever(pluginRespirator.node).thenReturn(node)
-               setField(clientCore, "uskManager", uskManager)
-               setField(clientCore, "clientContext", mock<ClientContext>())
+               whenever(clientCore.getUskManager()).thenReturn(uskManager)
+               whenever(clientCore.getClientContext()).thenReturnMock()
        }
 
        @Test