# JUnit Tests with Processing in Eclipse

**URL:** <https://discourse.processing.org/t/junit-tests-with-processing-in-eclipse/2426>\
**Category:** Processing\
**Created:** [August 6, 2018, 9:01pm UTC](https://discourse.processing.org/t/junit-tests-with-processing-in-eclipse/2426 "2018-08-06T21:01:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![widemouthfrog1](https://avatars.discourse-cdn.com/v4/letter/w/46a35a/32.png) [@widemouthfrog1](https://discourse.processing.org/u/widemouthfrog1)\
**Post date:** [August 6, 2018, 9:01pm UTC](https://discourse.processing.org/t/junit-tests-with-processing-in-eclipse/2426/1 "2018-08-06T21:01:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [August 6, 2018, 9:25pm UTC](https://discourse.processing.org/t/junit-tests-with-processing-in-eclipse/2426/2 "2018-08-06T21:25:43Z")

</div>

What exactly are you trying to test?

---

<div class="post-metadata">

**Author:** ![widemouthfrog1](https://avatars.discourse-cdn.com/v4/letter/w/46a35a/32.png) [@widemouthfrog1](https://discourse.processing.org/u/widemouthfrog1)\
**Post date:** [August 6, 2018, 9:31pm UTC](https://discourse.processing.org/t/junit-tests-with-processing-in-eclipse/2426/3 "2018-08-06T21:31:28Z")

</div>

I’m trying to test wall collisions in a game I’m making

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [August 6, 2018, 9:51pm UTC](https://discourse.processing.org/t/junit-tests-with-processing-in-eclipse/2426/4 "2018-08-06T21:51:51Z")

</div>

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:

```auto
public class CollisionChecker{
  public boolean checkCollision(Rectangle one, Rectangle two){
    // check collisions here
  }
}

```

Then in your Processing sketch, you could do something like this:

```auto
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.

---

<div class="post-metadata">

**Author:** ![widemouthfrog1](https://avatars.discourse-cdn.com/v4/letter/w/46a35a/32.png) [@widemouthfrog1](https://discourse.processing.org/u/widemouthfrog1)\
**Post date:** [August 6, 2018, 9:58pm UTC](https://discourse.processing.org/t/junit-tests-with-processing-in-eclipse/2426/5 "2018-08-06T21:58:37Z")

</div>

Thanks, that’s going to take a fair bit of work for what I’m doing but it should work

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [August 6, 2018, 10:01pm UTC](https://discourse.processing.org/t/junit-tests-with-processing-in-eclipse/2426/6 "2018-08-06T22:01:32Z")

</div>

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. 😉

---

<div class="post-metadata">

**Author:** ![widemouthfrog1](https://avatars.discourse-cdn.com/v4/letter/w/46a35a/32.png) [@widemouthfrog1](https://discourse.processing.org/u/widemouthfrog1)\
**Post date:** [August 6, 2018, 10:06pm UTC](https://discourse.processing.org/t/junit-tests-with-processing-in-eclipse/2426/7 "2018-08-06T22:06:55Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [August 6, 2018, 11:04pm UTC](https://discourse.processing.org/t/junit-tests-with-processing-in-eclipse/2426/8 "2018-08-06T23:04:05Z")

</div>

Does Java provides a JUinit/assertion library? @Kevin

Kf

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [August 6, 2018, 11:08pm UTC](https://discourse.processing.org/t/junit-tests-with-processing-in-eclipse/2426/9 "2018-08-06T23:08:59Z")

</div>

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.)
