Testing - initialize data folder in WrapperMock; create ReflectionUtils
- Change AuthMeMockUtils (reflection-based test setup) to ReflectionUtils: service providing reflection functionalities for particular tests where it is appropriate; - Initialize the data folder (required as soon as the Settings class is loaded) immediately in WrapperMock. Some tests did not set it up that required it and it goes unnoticed if the test is not run individually. This will hopefully fix the tests from failing in the Jenkins build.
This commit is contained in:
@@ -1,22 +1,16 @@
|
||||
package fr.xephi.authme.util;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.AuthMeMockUtil;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collection;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
@@ -41,7 +35,6 @@ public class UtilsTest {
|
||||
@BeforeClass
|
||||
public static void setUpMocks() {
|
||||
wrapperMock = WrapperMock.createInstance();
|
||||
wrapperMock.setDataFolder(new File("/"));
|
||||
authMeMock = wrapperMock.getAuthMe();
|
||||
}
|
||||
|
||||
@@ -119,11 +112,12 @@ public class UtilsTest {
|
||||
// Note ljacqu 20151122: This is a heavy test setup with reflections... If it causes trouble, skip it with @Ignore
|
||||
public void shouldRetrieveListOfOnlinePlayersFromReflectedMethod() {
|
||||
// given
|
||||
setField("getOnlinePlayersIsCollection", false);
|
||||
ReflectionTestUtils.setField(Utils.class, null, "getOnlinePlayersIsCollection", false);
|
||||
try {
|
||||
setField("getOnlinePlayers", UtilsTest.class.getDeclaredMethod("onlinePlayersImpl"));
|
||||
ReflectionTestUtils.setField(Utils.class, null, "getOnlinePlayers",
|
||||
UtilsTest.class.getDeclaredMethod("onlinePlayersImpl"));
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException("Cannot initialize test with custom test method", e);
|
||||
throw new RuntimeException("Could not get method onlinePlayersImpl() in test class", e);
|
||||
}
|
||||
|
||||
// when
|
||||
@@ -133,16 +127,6 @@ public class UtilsTest {
|
||||
assertThat(players, hasSize(2));
|
||||
}
|
||||
|
||||
private static void setField(String name, Object value) {
|
||||
try {
|
||||
Field field = Utils.class.getDeclaredField(name);
|
||||
field.setAccessible(true);
|
||||
field.set(null, value);
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
throw new RuntimeException("Could not set field '" + name + "'", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Note: This method is used through reflections
|
||||
@SuppressWarnings("unused")
|
||||
public static Player[] onlinePlayersImpl() {
|
||||
|
||||
@@ -21,14 +21,14 @@ public class WrapperMock extends Wrapper {
|
||||
|
||||
private Map<Class<?>, Object> mocks = new HashMap<>();
|
||||
private static WrapperMock singleton;
|
||||
private File getDataFolderValue;
|
||||
private File getDataFolderValue = new File("/");
|
||||
|
||||
private WrapperMock() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the WrapperMock and inject it as singleton into Wrapper.
|
||||
* Create a new instance of the WrapperMock and inject it as singleton into the Wrapper class.
|
||||
*
|
||||
* @return The created singleton
|
||||
*/
|
||||
@@ -38,16 +38,6 @@ public class WrapperMock extends Wrapper {
|
||||
return singleton;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the WrapperMock singleton or null if it hasn't been initialized. To avoid confusion, it may be best to
|
||||
* only call {@link WrapperMock#createInstance()} and to keep a reference to the returned singleton.
|
||||
*
|
||||
* @return The singleton or null
|
||||
*/
|
||||
public static WrapperMock getInstance() {
|
||||
return singleton;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Logger getLogger() {
|
||||
return getMock(Logger.class);
|
||||
@@ -86,6 +76,12 @@ public class WrapperMock extends Wrapper {
|
||||
return getMock(File.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the data folder to be returned for test contexts. Defaults to File("/"); supply null to make WrapperMock
|
||||
* return a mock of the File class as with the other fields.
|
||||
*
|
||||
* @param file The data folder location to return
|
||||
*/
|
||||
public void setDataFolder(File file) {
|
||||
this.getDataFolderValue = file;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user