aspose file tools*
The moose likes Testing and the fly likes JUnit Testing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "JUnit Testing" Watch "JUnit Testing" New topic
Author

JUnit Testing

Jacqueline Turof
Greenhorn

Joined: Apr 18, 2013
Posts: 12
Hi,

I'm suppose to be creating a JUnit test 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.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Here is a good Tutorial: http://www.vogella.com/articles/JUnit/article.html

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.


Steve
Jacqueline Turof
Greenhorn

Joined: Apr 18, 2013
Posts: 12
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)
{
super(message);
}
}
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

What is the exact and complete error message you get?
Jacqueline Turof
Greenhorn

Joined: Apr 18, 2013
Posts: 12
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?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

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.



Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

If you're trying to test your constructor, don't do it in the setup. Invoke the constructor in a test method, like so:

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

Also, I don't see a need to have "Logic" in the class name. "Hangman" is enough. Every program involves some logic so adding "Logic" to the name of your class is redundant.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

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"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JUnit Testing