java在libgdx中为junit测试加载资产

cotxawn7  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(407)

我正在用libgdx和java开发一个游戏。我想在我的游戏的一些特性上运行一些junit测试,但是我在加载我的资产时遇到了困难。我已经尝试实现mockito,这样我就可以进行无头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文件夹成为该模块的resources根目录,但仍然没有加载该文件。
有什么建议吗?

ovfsdjhp

ovfsdjhp1#

我不知道如何在fireengine类中使用纹理,但您可能需要像在游戏中一样通过assetmanager加载和访问纹理。

相关问题