简体   繁体   English

使用intellij在libgdx中运行测试

[英]running tests in libgdx using intellij

my team and myself are creating an app using libgdx, and we keep some of our data in a json file. 我的团队和我自己正在使用libgdx创建一个应用,我们将一些数据保存在json文件中。 Nothing special. 没什么特别的。 the file is kept in "android/assets/data/cards.json", so the Gdx.files.internal command can find it easily in android (naturally) and when running the desktop version (after changing the working directory). 该文件保存在“ android / assets / data / cards.json”中,因此Gdx.files.internal命令可以在android中(自然地)以及在运行桌面版本(更改工作目录后)时轻松找到它。 Problem is, when running the tests in intellij the working directory change doesn't apply, so it can't find it. 问题是,在intellij中运行测试时,工作目录更改不适用,因此无法找到它。 I tried to make a copy of said file into the core project. 我试图将所述文件的副本复制到核心项目中。 so I have these 2 ways of reading the file: 所以我有以下两种读取文件的方式:

public static final String cardsJsonFilePath = "data/cards.json"; /**** at a later method ****/ byte[] encoded = (Gdx.files.internal(cardsJsonFilePath)).readBytes(); byte[] encoded = Files.readAllBytes(Paths.get(cardsJsonFilePath));

the first assignment works in every configuration except the tests (which recieve a null from GDX.files.internal) and the second one works as long as I keep the copy of the data folder in the core directory, and not on android (since Files and Paths appearantly do not exist in android). 第一个分配在除测试(从GDX.files.internal接收到一个空值)之外的所有配置中都有效,第二个分配则只要我将数据文件夹的副本保留在核心目录中而不是在android上就可以工作(因为Files并且路径显然在android中不存在)。 also, the latter is a rather poor solution since it forces me to keep an updated copy of the file for tests sake. 同样,后者是一个较差的解决方案,因为它迫使我为测试目的保留文件的更新副本。

TL ; TL; DR I have an asset that I want my tests to access in intellij, and I want it to work with libgdx.files.internal or some other android friendly solution. DR,我有一项资产,我希望我的测试可以在intellij中访问,并且我希望它可以与libgdx.files.internal或其他一些Android友好解决方案一起使用。

thanks in advance! 提前致谢!

so, I found out the answer to my own question, so if you have a similar problem I hope this helps. 因此,我找到了自己的问题的答案,因此,如果您遇到类似的问题,希望对您有所帮助。 I was wrong when I suspected that the null pointer exception was due to not finding the file. 当我怀疑空指针异常是由于找不到文件而导致的时,我错了。 the reason for the exception is that the Gdx class is not properly initialized when not run in a proper application context (for example, when used for a test). 发生异常的原因是,如果未在适当的应用程序上下文中运行(例如,用于测试时),则Gdx类未正确初始化。 so the null was not the result of the internal method, but Gdx.files itself was null. 因此null不是内部方法的结果,但Gdx.files本身为null。

in short, you can't just use libgdx library methods when you feel like, it's a framework, not a library. 简而言之,您不能只是在感觉上使用libgdx库方法,而是一个框架,而不是一个库。

the workaround we used was this: byte[] encoded; if (Gdx.files == null) encoded = Files.readAllBytes(Paths.get(cardsJsonFilePath)); else encoded = (Gdx.files.internal(cardsJsonFilePath)).readBytes(); return new String(encoded); 我们使用的解决方法是: byte[] encoded; if (Gdx.files == null) encoded = Files.readAllBytes(Paths.get(cardsJsonFilePath)); else encoded = (Gdx.files.internal(cardsJsonFilePath)).readBytes(); return new String(encoded); byte[] encoded; if (Gdx.files == null) encoded = Files.readAllBytes(Paths.get(cardsJsonFilePath)); else encoded = (Gdx.files.internal(cardsJsonFilePath)).readBytes(); return new String(encoded); so the andoird application doesn't crash because it never uses the files and paths part of the code (I'm somewhat surprised that it compiles, but meh. not gonna argue with a good thing, right? ) and the tests can still use it in place of the null Gdx.files field. 因此andoird应用程序不会崩溃,因为它从未使用过代码的文件和路径部分(我对此感到有些惊讶,但是它可以编译,但是不会与一件好事争论,对吗?)并且测试仍然可以使用它代替空的Gdx.files字段。 so everybody wins! 这样每个人都赢了!

have fun building (and testing!) your games! 畅玩游戏(并进行测试!)! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM