#StackBounty: #unit-testing #kotlin #libgdx Unit Testing Libgdx classes that call a ShaderProgram (such as a Stage) via a HeadlessAppli…

Bounty: 50

I am trying to unit test a the core package of my libgdx application.

What is the best way to mock the ShaderProgram such that the root class may be tested?

Given the following init for a Libgdx test runner,

init {
    val conf = HeadlessApplicationConfiguration()

    HeadlessApplication(this, conf)
    Gdx.gl = mock(GL20::class.java) 
    Gdx.gl20 = mock(GL20::class.java)
    Gdx.gl30 = mock(GL30::class.java)
    Gdx.graphics = mock(Graphics::class.java)
    `when`(Gdx.graphics.height).thenReturn(dimensions)
    `when`(Gdx.graphics.width).thenReturn(dimensions)
}

and the function under test (which is in a class that is a child of Application Listener),

override fun create() {
    ...
    stage = Stage(ScreenViewport())
    ...
}

an error occurs inside of Stage when it attempts to compile a shader.

I.e., in SpriteBatch.java from com.badlogic.gdx.graphics.g2d,

ShaderProgram shader = new ShaderProgram(vertexShader, fragmentShader);
if (shader.isCompiled() == false) throw new IllegalArgumentException("Error compiling shader: " + shader.getLog());

shader.isCompiled() appears to always return false for a HeadlessApplication.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.