Sharing test preparation

This commit is contained in:
Norbi Peti 2017-04-14 22:40:25 +02:00
parent 0160b4146f
commit 3bab090fc7
2 changed files with 39 additions and 24 deletions

View file

@ -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<Object>() {
@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());
}
}));
}
}

View file

@ -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<Object>() {
@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");