FIX cardDb tests in Maven for ImageKeys Mock configuration between subclasses

This commit is contained in:
leriomaggio
2021-09-13 10:15:32 +01:00
parent 99fdd4b551
commit 236fd99862
4 changed files with 38 additions and 22 deletions

View File

@@ -1,20 +1,25 @@
package forge.card;
import com.google.common.base.Predicate;
import forge.ImageCache;
import forge.ImageKeys;
import forge.Singletons;
import forge.StaticData;
import forge.item.IPaperCard;
import forge.item.PaperCard;
import forge.localinstance.properties.ForgePreferences;
import forge.model.FModel;
import forge.util.Localizer;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import javax.imageio.ImageIO;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.*;
import static org.testng.Assert.*;
@@ -2138,5 +2143,4 @@ public class CardDbTestCase extends ForgeCardMockTestCase {
assertTrue(islandCard.isFoil());
}
}
}

View File

@@ -1,5 +1,6 @@
package forge.card;
import forge.ImageCache;
import forge.ImageKeys;
import forge.item.PaperCard;
import org.mockito.Mockito;
@@ -7,6 +8,8 @@ import org.powermock.api.mockito.PowerMockito;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import javax.imageio.ImageIO;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
@@ -26,11 +29,11 @@ public class CardDbTestWithNoImage extends CardDbTestCase {
}
@Override
@BeforeMethod
protected void initMocks() throws Exception {
protected void initCardImageMocks() {
PowerMockito.mockStatic(ImageIO.class);
PowerMockito.mockStatic(ImageCache.class);
PowerMockito.mockStatic(ImageKeys.class);
PowerMockito.when(ImageKeys.hasImage(Mockito.any(PaperCard.class))).thenReturn(false);
super.initMocks();
PowerMockito.when(ImageKeys.hasImage(Mockito.any(PaperCard.class), Mockito.anyBoolean())).thenReturn(false);
}
@Test

View File

@@ -114,7 +114,7 @@ public class ForgeCardMockTestCase extends PowerMockTestCase {
fLangDir.set(ForgeConstants.class, langDir);
}
private void setMock(Localizer mock) {
protected void setMock(Localizer mock) {
try {
Field instance = Localizer.class.getDeclaredField("instance");
instance.setAccessible(true);
@@ -128,16 +128,14 @@ public class ForgeCardMockTestCase extends PowerMockTestCase {
protected void initMocks() throws Exception {
//Loading a card also automatically loads the image, which we do not want (even if it wouldn't cause exceptions).
//The static initializer block in ImageCache can't fully be mocked (https://code.google.com/p/powermock/issues/detail?id=256), so we also need to mess with ImageIO...
//TODO: make sure that loading images only happens in a GUI environment, so we no longer need to mock this
PowerMockito.mockStatic(ImageIO.class);
PowerMockito.mockStatic(ImageCache.class);
PowerMockito.mockStatic(ImageKeys.class);
initCardImageMocks();
initForgeConstants();
// Always Has Image (there is a separated test case to cover the opposite case)
PowerMockito.when(ImageKeys.hasImage(Mockito.any(PaperCard.class))).thenReturn(true);
//Mocking some more static stuff
initForgePreferences();
initializeStaticData();
}
protected void initForgePreferences() throws IllegalAccessException {
PowerMockito.mockStatic(Singletons.class);
PowerMockito.mockStatic(FModel.class);
ForgePreferences forgePreferences = new ForgePreferences();
@@ -161,7 +159,14 @@ public class ForgeCardMockTestCase extends PowerMockTestCase {
PowerMockito.field(Localizer.class, "resourceBundle").set(localizerMock, dummyResourceBundle);
PowerMockito.when(localizerMock.getMessage(Mockito.anyString())).thenReturn("any string");
PowerMockito.when(FModel.getPreferences()).thenReturn(forgePreferences);
initializeStaticData();
}
protected void initCardImageMocks() {
//make sure that loading images only happens in a GUI environment, so we no longer need to mock this
PowerMockito.mockStatic(ImageIO.class);
PowerMockito.mockStatic(ImageCache.class);
PowerMockito.mockStatic(ImageKeys.class);
PowerMockito.when(ImageKeys.hasImage(Mockito.any(PaperCard.class), Mockito.anyBoolean())).thenReturn(true);
}
protected void initializeStaticData() {