I'm suppose to be creating a JUnittest and I feel like I don't really understand these JUnit tests at all.
Does anyone know of a good tutorial to look at? I've been looking around and have been unable to find one.
This assignment is due Sunday and at this point I'm not even sure how to begin tackling this problem. Thanks, for any pointers or help.
Before you begin: I have seen lots of people (me included) approach unit testing with fear and trepidation, as it were black magic. It isn't. The concept is simple. You have already written your code. You want to make sure your method behaves the way you expect. So you make another method which calls your code, passes in some known values, and gets the results. You then compare the results against what you expect, and make sure you get the correct value.
That is all unit testing is. The first example in the tutorial I posted is this:
Vogella.com wrote:The following code shows a JUnit test method which can be created via File → New → JUnit → JUnit Test case.
You could easily write that same unit test without JUnit:
So in the basic case all JUnit is a library with classes you use, which really isjust designed to:
1) Make it easier to launch tests
2) Make it easier to make comparisons
3) Make the output consistent
4) Save a lot of boilerplate in your test code
And because of that, it makes it easy to integrate with tools like IDEs. But you should take out of it: all you need to do is write code, just like all your other Java code, which exercises the method(s) of your application. Nothing to worry about, nothing particular special or worrisome, and nothing to be apprehensive about knowing: it really isn't a new skill set to learn, just some new methods.
Joined: Apr 18, 2013
Thanks for your reply and with that information I was able to make some simple JUnits run and work.
Now, I'm getting stuck on more complex testing. I am trying to test the constructor for my HangmanLogic class. The
constructor is public HangmanLogic(String keyPhrase, int numberOfGuesses) throws InvalidConfigurationException
Just trying to start the testing with making sure the information being passed into the constructor is what it should be. I
keep seeing an initializationError.
public class AlreadyGuessedException extends Exception
public AlreadyGuessedException(String message)
What is the exact and complete error message you get?
Joined: Apr 18, 2013
On the side where the JUnit bar is, it says there is one error and lists it as initializationError(0.000 s). So I'm assuming I'm not setting up the constructor right at all. I didn't use the @Before in any of my other JUnit attempts when I was able to get them working. Maybe I'm not using it properly?
Possibly. See if you can dig more information out. An error isn't going to be shown with that little information. Check your console, the IDE's console, or other output areas and see if anything gets displayed. Which version of the JUnit Test Case are you creating?
Check your constructor logic. The equals() method always returns false when the argument is null. Besides, when keyPhrase is actually null, you'll get a NullPointerException. Use keyPhrase == null instead and make that the first expression instead of the second, otherwise keyphrase.equals("") will throw a NullPointerException before you can even check for null.
A little off-topic: It's good that you're writing JavaDoc comments but you should check out the JavaDocs style guide. Specifically, you should edit your JavaDoc comments that start with "This method..." and start with a verb-phrase instead. For @returns, you don't need to start the description with "returns" -- it's redundant.
Note how you would refer to the current Hangman object as "this game" or "this Hangman"