Add methods to Reflection utils; test ChangePasswordCommand fully

This commit is contained in:
ljacqu
2015-11-28 20:40:02 +01:00
parent c02bf7db76
commit a1a14aa760
6 changed files with 106 additions and 46 deletions
@@ -23,7 +23,6 @@ import static org.mockito.Mockito.*;
*/
public class UtilsTest {
private static WrapperMock wrapperMock;
private static AuthMe authMeMock;
private PermissionsManager permissionsManagerMock;
@@ -34,7 +33,7 @@ public class UtilsTest {
*/
@BeforeClass
public static void setUpMocks() {
wrapperMock = WrapperMock.createInstance();
WrapperMock wrapperMock = WrapperMock.createInstance();
authMeMock = wrapperMock.getAuthMe();
}
@@ -109,16 +108,11 @@ public class UtilsTest {
}
@Test
// Note ljacqu 20151122: This is a heavy test setup with reflections... If it causes trouble, skip it with @Ignore
public void shouldRetrieveListOfOnlinePlayersFromReflectedMethod() {
// given
ReflectionTestUtils.setField(Utils.class, null, "getOnlinePlayersIsCollection", false);
try {
ReflectionTestUtils.setField(Utils.class, null, "getOnlinePlayers",
UtilsTest.class.getDeclaredMethod("onlinePlayersImpl"));
} catch (NoSuchMethodException e) {
throw new RuntimeException("Could not get method onlinePlayersImpl() in test class", e);
}
ReflectionTestUtils.setField(Utils.class, null, "getOnlinePlayers",
ReflectionTestUtils.getMethod(UtilsTest.class, "onlinePlayersImpl"));
// when
Collection<? extends Player> players = Utils.getOnlinePlayers();
@@ -20,8 +20,6 @@ import java.util.logging.Logger;
public class WrapperMock extends Wrapper {
private Map<Class<?>, Object> mocks = new HashMap<>();
private static WrapperMock singleton;
private File getDataFolderValue = new File("/");
private WrapperMock() {
super();
@@ -33,9 +31,9 @@ public class WrapperMock extends Wrapper {
* @return The created singleton
*/
public static WrapperMock createInstance() {
singleton = new WrapperMock();
Wrapper.setSingleton(singleton);
return singleton;
WrapperMock instance = new WrapperMock();
Wrapper.setSingleton(instance);
return instance;
}
@Override
@@ -70,20 +68,19 @@ public class WrapperMock extends Wrapper {
@Override
public File getDataFolder() {
if (singleton.getDataFolderValue != null) {
return singleton.getDataFolderValue;
}
return getMock(File.class);
return new File("/");
}
/**
* 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.
* Return whether a mock of the given class type was created, i.e. verify whether a certain method was executed on
* the Wrapper to retrieve an entity.
*
* @param file The data folder location to return
* @param mockClass The class of the mock to verify
*
* @return True if the mock has been created, false otherwise
*/
public void setDataFolder(File file) {
this.getDataFolderValue = file;
public boolean wasMockCalled(Class<?> mockClass) {
return mocks.get(mockClass) != null;
}
@SuppressWarnings("unchecked")