Creating Texture in headless LibGDX unit tests

允我心安 提交于 2019-12-12 09:37:13

问题


I am using the LibGDX headless backend to run jUnit tests. This works well for certain tests, but if I try to create a new Texture('myTexture.png');, I get a NullPointerException. The exact error is:

java.lang.NullPointerException
    at com.badlogic.gdx.graphics.GLTexture.createGLHandle(GLTexture.java:207)

To keep things simple, I created a method that does nothing other than load a texture:

public class TextureLoader {
    public Texture load(){
        return new Texture("badlogic.jpg");
    }
}

Then, my test class looks like this:

public class TextureTest {

    @Before
    public void before(){
        final HeadlessApplicationConfiguration config = new HeadlessApplicationConfiguration();
        new HeadlessApplication(new ApplicationListener() {
            // Override necessary methods
            ...
        }, config);
    }

    @Test
    public void shouldCreateTexture() {
        TextureLoader loader = new TextureLoader();
        assertNotNull( loader.load() );
    }
}

This method is working correctly in my actual app, just not in the unit tests.

How can I use the HeadlessApplication class to load Textures?


回答1:


Mocking the Gdx.gl help me solved this NullPointerException during Texture creation:

import static org.mockito.Mockito.mock;

...

Gdx.gl = mock(GL20.class);

I used it with GdxTestRunner, see https://bitbucket.org/TomGrill/libgdx-testing-sample

public GdxTestRunner(Class<?> klass) throws InitializationError {
    super(klass);
    HeadlessApplicationConfiguration conf = new HeadlessApplicationConfiguration();
    new HeadlessApplication(this, conf);
    Gdx.gl = mock(GL20.class); // my improvement
}


来源:https://stackoverflow.com/questions/25612660/creating-texture-in-headless-libgdx-unit-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!