Texture Creation in Libless Free LibGDX Unit Tests

I am using the headless libgdx server 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. Exact error:

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

For simplicity, I created a method that does nothing but load the 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 works correctly in my real application, but not in unit tests.

How can I use the HeadlessApplication class to load textures?

+8
junit opengl libgdx headless
source share
1 answer

Taunting Gdx.gl helped me solve this NullPointerException when creating a texture:

 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 } 
+8
source share

All Articles