Reputation: 11
I have been attempting to use the headless-backend library to help create unit tests for my own subclass (MainMenuScreen) of the ScreenAdapter class, but A) MainMenuScreen uses the SpriteBatch class in its initialization, and as far as I know there isn't a way to mock that, and B) I'm not entirely sure if I'm setting up the headless application correctly. I am using Maven for dependencies, JUnit for testing, and JaCoco for test coverage info.
My question(s): how do I set up a headless application for MainMenuClass? If that can't be done, is there some other way to unit test classes that use LibGDX and OpenGL methods/classes (especially SpriteBatch)?
What I have already researched:
My unit test code:
public class MainMenuTest {
final MazeGame testGame = mock(MazeGame.class);
private MainMenuScreen testScreen;
private HeadlessApplication app;
// TODO: get the headless backend working; the current problem is getting OpenGL methods working
u/BeforeEach
public void setup() {
MockGraphics mockGraphics = new MockGraphics();
Gdx.graphics = mockGraphics;
Gdx.gl = mock(GL20.class);
testScreen = new MainMenuScreen(testGame);
HeadlessApplicationConfiguration config = new HeadlessApplicationConfiguration();
app = new HeadlessApplication(new ApplicationListener() {
u/Override
public void create() {
// Set the screen to MainMenuScreen directly
testScreen = new MainMenuScreen(testGame); // Pass null or a mock game instance if necessary
}
u/Override
public void resize(int width, int height) {}
u/Override
public void render() {
testScreen.render(1 / 60f); // Simulate a frame render
}
u/Override
public void pause() {}
u/Override
public void resume() {}
u/Override
public void dispose() {
testScreen.dispose();
}
}, config);
}
/**
* Test to see if the start button works.
*/
u/Test
public void startButtonWorks() {
// doesn't click start button
Button startButton = testScreen.getStartButton();
assertEquals(false, startButton.isChecked());
// clicks start button
((ChangeListener) (startButton.getListeners().first())).changed(new ChangeEvent(), startButton);
assertEquals(true, startButton.isChecked());
}
}
Current error during mvn test: enter image description here
Thanks in advance, and let me know if this isn't the right place to post this/I need to provide more info.
Upvotes: 1
Views: 49