9

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?

dStulle
  • 609
  • 5
  • 24
twiz
  • 9,041
  • 8
  • 52
  • 84
  • The libgdx Texture class basically wraps an OpenGL Texture handle. The headless backend has no OpenGL context (hence the "headless"). You can't create an OpenGL Texture handle without OpenGL context. – Xoppa Sep 01 '14 at 21:08
  • Ok, that makes sense. I guess this is more of a general java question, but do you know of a good way to handle this in unit tests? Dependency injection could be used, but passing in every texture as a parameter sounds messy. – twiz Sep 01 '14 at 21:57
  • 2
    For now, I've solved my problem by just using lwjgl instead of the headless backend. This seems counter-intuitive, since I assume unit testing is the main purpose for the existence of a headless backend. I understand that an actual headless openGL implementation would be pointless, but wouldn't it make sense for the headless backend to contain a mock version of the API to prevent exceptions? – twiz Sep 02 '14 at 15:14
  • @twiz I totally agree with you. I'm currently facing this problem myself. Haven't entirely gotten it to work, could you add a self-answer to your question with your JUnit test code? – Simon Forsberg Dec 21 '14 at 23:47

1 Answers1

8

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
}
dStulle
  • 609
  • 5
  • 24
Bill Lin
  • 1,145
  • 1
  • 11
  • 12
  • 1
    I completely gave up on LibGDX and never actually tried this, but based on the upvotes, I'm going to accept the answer. – twiz Jul 19 '19 at 17:39