admin管理员组文章数量:1023876
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:
- .badlogicgames.gdx/gdx-backend-headless/latest/index.html: gdx-backend JavaDocumentation; very sparse, with no examples or explanations of how the code works.
- Is there any way to create integration test for libGDX application?: the answer right below the question has the best example I can find of how to use HeadlessApplication in the setup method. However, I think it requires me to have created a headless version of the class I want to test; I don't know how to do that.
- : Tom Grill's gdx-testing library. I'm 99% sure all this does is take LibGDX dependent code and turn it into a headless application, but every attempt I have made to use it has failed miserably.
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.
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:
- .badlogicgames.gdx/gdx-backend-headless/latest/index.html: gdx-backend JavaDocumentation; very sparse, with no examples or explanations of how the code works.
- Is there any way to create integration test for libGDX application?: the answer right below the question has the best example I can find of how to use HeadlessApplication in the setup method. However, I think it requires me to have created a headless version of the class I want to test; I don't know how to do that.
- : Tom Grill's gdx-testing library. I'm 99% sure all this does is take LibGDX dependent code and turn it into a headless application, but every attempt I have made to use it has failed miserably.
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.
本文标签:
版权声明:本文标题:java - How do I make unit tests for a LibGDX application that uses SpriteBatch and other OpenGL methodsclasses? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745569182a2156633.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论