I’m trying to get JUnit tests working with Processing but I’m not sure how to go about it. Every way I’ve tried either ends up with a ClassNotFoundException, or says size() cannot be used here (when I use Main app = new Main() followed by app.settings())
Is there a way to get this working?
What exactly are you trying to test?
I’m trying to test wall collisions in a game I’m making
It’s not exactly trivial to write unit tests for Processing (or for user interfaces in general).
If you really want to write a unit test for your code, here’s what I would do:
- Extract your logic into separate classes that don’t care about rendering. For example, your collision detection shouldn’t have to care about your rendering code.
- Write unit tests for those separate classes.
- Optionally, to test the user interface, consider outputting your sketch as a series of screenshots and comparing the screenshots to each other.
For example, maybe you’d have a class like this:
public class CollisionChecker{
public boolean checkCollision(Rectangle one, Rectangle two){
// check collisions here
}
}
Then in your Processing sketch, you could do something like this:
void draw(){
if(collisionChecker.checkCollision(playerRectangle, enemyRectangle){
gameOver();
}
playerRectangle.draw();
enemyRectangle.draw();
}
Then you could write unit tests for CollisionChecker
without worrying about any rendering code.
Thanks, that’s going to take a fair bit of work for what I’m doing but it should work
Out of curiosity, what context are you coming from where you want to write unit tests for Processing? Most people who write in Processing would just skip the unit tests.
There’s a lot of maths involved in what I’m doing and a great possibility that when I add something that it could stuff up code elsewhere and I wouldn’t know where to start looking to find the problem. This way with the JUnit tests, if I break something I thought was working I’ll be notified so I can avoid trying to search through walls of code for an error that originated elsewhere. (I’m also having trouble finding the solution the error I have currently with just the help of the debugger)
Does Java provides a JUinit/assertion library? @Kevin
Kf
Yeah, JUnit is a Java library that you use to do unit testing. (I have a tutorial on this ready to go, I just haven’t posted it yet.)