From 3bab090fc7741c0c172a78b5ef09d14d4ebcef25 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 14 Apr 2017 22:40:25 +0200 Subject: [PATCH] Sharing test preparation --- .../java/buttondevteam/core/TestPrepare.java | 38 +++++++++++++++++++ .../buttondevteam/core/PlayerDataTest.java | 25 +----------- 2 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 src/main/java/buttondevteam/core/TestPrepare.java diff --git a/src/main/java/buttondevteam/core/TestPrepare.java b/src/main/java/buttondevteam/core/TestPrepare.java new file mode 100644 index 0000000..e135a6a --- /dev/null +++ b/src/main/java/buttondevteam/core/TestPrepare.java @@ -0,0 +1,38 @@ +package buttondevteam.core; + +import java.util.Collection; +import java.util.Collections; +import java.util.logging.Logger; + +import org.bukkit.Bukkit; +import org.bukkit.Server; +import org.bukkit.plugin.PluginManager; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class TestPrepare { + public static void PrepareServer() { + Bukkit.setServer(Mockito.mock(Server.class, new Answer() { + + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + // System.out.println("Return type: " + invocation.getMethod().getReturnType()); + // System.out.println(String.class.isAssignableFrom(invocation.getMethod().getReturnType())); + if (returns(invocation, String.class)) + return "test"; + if (returns(invocation, Logger.class)) + return Logger.getAnonymousLogger(); + if (returns(invocation, PluginManager.class)) + return Mockito.mock(PluginManager.class); + if (returns(invocation, Collection.class)) + return Collections.EMPTY_LIST; + return null; + } + + boolean returns(InvocationOnMock invocation, Class cl) { + return cl.isAssignableFrom(invocation.getMethod().getReturnType()); + } + })); + } +} diff --git a/src/test/java/buttondevteam/core/PlayerDataTest.java b/src/test/java/buttondevteam/core/PlayerDataTest.java index a512e82..7bf9ed0 100644 --- a/src/test/java/buttondevteam/core/PlayerDataTest.java +++ b/src/test/java/buttondevteam/core/PlayerDataTest.java @@ -1,15 +1,6 @@ package buttondevteam.core; import java.util.UUID; -import java.util.logging.Logger; - -import org.bukkit.Bukkit; -import org.bukkit.Server; -import org.bukkit.plugin.PluginManager; -import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; - import buttondevteam.core.TestPlayerClass.TestEnum; import buttondevteam.lib.player.TBMCPlayerBase; import junit.framework.Test; @@ -29,21 +20,7 @@ public class PlayerDataTest extends TestCase { } public void testConfig() { - Bukkit.setServer(Mockito.mock(Server.class, new Answer() { - - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - // System.out.println("Return type: " + invocation.getMethod().getReturnType()); - // System.out.println(String.class.isAssignableFrom(invocation.getMethod().getReturnType())); - if (String.class.isAssignableFrom(invocation.getMethod().getReturnType())) - return ""; - if (Logger.class.isAssignableFrom(invocation.getMethod().getReturnType())) - return Logger.getAnonymousLogger(); - if (PluginManager.class.isAssignableFrom(invocation.getMethod().getReturnType())) - return Mockito.mock(PluginManager.class); - return null; - } - })); + TestPrepare.PrepareServer(); UUID uuid = new UUID(0L, 0L); try (TestPlayerClass p = TBMCPlayerBase.getPlayer(uuid, TestPlayerClass.class)) { p.PlayerName().set("Test");