gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:06:04 25 4
gpt4 key购买 nike

我目前正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com