java - 在 libgdx 中加载资源以进行 JUnit 测试

标签 java junit libgdx

我目前正在使用 libgdx 和 java 开发游戏。我想对我的游戏的某些功能运行一些 JUnit 测试,但是我在加载 Assets 时遇到困难。我尝试实现 Mockito,以便我可以进行 headless JUnit 测试,看看这是否有助于解决问题,但这并没有帮助。这是我尝试在其中加载资源的测试类(资源正在第 17 行加载)

import com.kroy.game.FireEngine;
import com.kroy.game.Fortress;
import com.kroy.game.Tile;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@RunWith(GdxTestRunner.class)
public class FireEngineTests {

    Tile testTile = new Tile();
    FireEngine testFireEngine = new FireEngine(100, 10, 10, testTile, 10, 100, "fireEngineSprite.png");
    Class ReflectionClass = FireEngine.class;

    @Test
    public void testRefillAmount() {
        testFireEngine.refillAmount(50);
        assertEquals(50, testFireEngine.getWaterAmount());
        testFireEngine.refillAmount(5000);
        assertEquals(100, testFireEngine.getWaterAmount());

    }


    @Test
    public void testCanShoot() {

    }


    @Test
    public void testDeath() {
        testFireEngine.setHealth(0);
        assertTrue(testFireEngine.isDisabled());
    }

    @Test
    public void testTakeDamage() {
        try {
            testFireEngine.setHealth(100);
            Method takeDamageReflection = ReflectionClass.getDeclaredMethod("takeDamage");
            takeDamageReflection.setAccessible(true);
            takeDamageReflection.invoke(50, takeDamageReflection);
            assertEquals(50, testFireEngine.getHealth());

            takeDamageReflection.invoke(5000, takeDamageReflection);
            assertEquals(0, testFireEngine.getHealth());

        } catch (NoSuchMethodException e) {

        } catch (InvocationTargetException e) {

        } catch (IllegalAccessException e) {

        }
    }

    @Test
    public void testTransferTo() {
        Tile newLocation = new Tile(2,2);
        try {
            Method transferToReflection = ReflectionClass.getDeclaredMethod("transferTo");
            transferToReflection.setAccessible(true);
            transferToReflection.invoke(newLocation, transferToReflection);
            assertEquals(testTile.getInhabitant(),null);
            assertEquals(newLocation.getInhabitant(), testFireEngine);

        } catch (NoSuchMethodException e) {

        } catch (InvocationTargetException e) {

        } catch (IllegalAccessException e) {

        }
    }

    @Test
    public void testShootTarget() {
        Fortress testFortress = new Fortress(1000, 10,2,testTile, "fortress1", "lavaTile.png");

        testFireEngine.shootTarget(testFortress);

        assertEquals(testFortress.getHealth(), (testFortress.getMaxHealth() - testFireEngine.getDamage()));
        assertEquals(99, testFireEngine.getWaterAmount());
    }
}

以下是我尝试运行的每个测试收到的错误消息:

com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: /Users/michaelShepherd/Documents/University Work/Year 2/SEPR/Assessment 3/Septagon3/tests/assets/fireEngineSprite.png

搜索到的文件肯定位于指定的位置,并且我还将 Assets 文件夹作为该模块的资源根目录,但该文件仍然无法加载。

有什么建议吗?

最佳答案

我不确定您如何在 FireEngine 类中使用纹理,但您可能需要像通常在游戏中一样通过 AssetManager 加载和访问纹理。

关于java - 在 libgdx 中加载资源以进行 JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60210932/

相关文章:

java - LibGDX Box2D RayCast fatal error

java - 如何找到所有基于图像的 PDF?

java - 在xml和java代码中创建对象的区别

Java DAO : null and Integer object or negative number and int in mapped object for optional number column?

java - 2 个 Java 正则表达式来验证 google DFP 条目

java - Derby数据库不支持@Column注释中的columnDefinition ="LONGTEXT"吗?

java - 使用mockito在抽象父类(super class)中设置私有(private)字段

java - 调试 Junit 时,Eclipse 中未命中断点

java - 大多数基本的 libgdx 应用程序崩溃

java - 如何优化 dlib 地标检测?