0

I have a pretty huge project where I'm trying to make a game. My classes are:

  • Game(main)
  • Player
  • Monster
  • Battleground
  • Item
  • InputReader
  • Utilities

There is a big chain of dependency, as some methods run others. The player class has methods like buyItem and SellItem, using Item objects from the class Item through a HashMap. Regardless, I am supposed to make a SetUp method in my unitTest class that makes objects for all the classes and puts Items into the players collection. I know how do to basic unit testing of individual methods, but I can't wrap my head around this. Please be nice, I know it might be obvious once I understand it.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
In6ify
  • 147
  • 2
  • 9
  • 1
    There's nothing special here: just write the code to create the objects, like you would in the main method of your game. – Andy Turner Oct 26 '16 at 13:05
  • 2
    Having not read your full question, it sounds like you are not unit- but [ingetration-testing](https://en.wikipedia.org/wiki/Integration_testing). You may want to take a look at [Object mocking](http://stackoverflow.com/questions/2128148/what-are-mock-objects-in-java) and only mock the dependencies you directly need. – Turing85 Oct 26 '16 at 13:07
  • 1
    Maybe you could isolate one issue which you want to test, Then this would be a more concrete question. – user140547 Oct 26 '16 at 13:11

2 Answers2

1

Create a method that does all the initializing and annotate it with @BeforeClass1

@BeforeClass
public static void initialize() {
    game = new Game()
    // further setting up
}

This method will run once, before all the unit tests defined in the class are executed.


1: There are more JUnit annotations under this link http://junit.sourceforge.net/javadoc/org/junit/package-summary.html

mike
  • 4,929
  • 4
  • 40
  • 80
  • 1
    Downvoted because this will not work until method is declared static. I will remove downvote when you fix that. – rkosegi Oct 26 '16 at 13:18
  • It only needs to be static when is uses static variables. I'm sure OP can adjust this to fit the use case. – mike Oct 26 '16 at 13:20
  • nope,wrong again. I would downvote again if I could. Did you tried it? `java.lang.Exception: Method setup() should be static` – rkosegi Oct 26 '16 at 13:23
  • 1
    Ah, seems you're right. Just tested it, JUnit demands the method to be static. – mike Oct 26 '16 at 13:29
0

You are looking at things from the wrong direction:

  1. You do not do unit-testing because somebody tells you "do it that way". You do it because it adds value to your development process. So, if there is no value-add, then don't do it (or most likely: step back and learn how to unit-testing in a way that helps you)
  2. The whole idea of unit tests is to work on the smallest possible unit. So when you find that your unit tests "need everything" all the time, then most likely ... you created a design that is hard to test. So this could be an indication to step back and look into your current design!

If you are really interested in getting value out of unit-testing, you should study resources, such as these videos that explain to write "testable code". Or material such as theartofunittesting.

In other words: writing unit test is a skill that needs to be trained. And especially the first steps are hard; but explaining all of those things would blow the scope of a SO question/answer.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • As I said, its a project (assignment) from my teacher. What I wrote is basically, word for word, what he asked me to do. I am just looking for input for this particular task. I will look more into the whole idea of unit testing when the assignment is done. Its sad, the time limit is always so small we don't really get the time to look into anything thoroughly before we have to move on to the next part of the assignment. Right now I just need to grasp the basic idea so I can move on and get it done before the time limit. – In6ify Oct 26 '16 at 13:24