This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "JUnit Testing." Watch "JUnit Testing." New topic

JUnit Testing.

Mike Lonsky

Joined: Feb 10, 2014
Posts: 11
I'm trying to do some JUnit testing for my Binary I/O. I've done some JUnit testing for getters & setters, but I have no idea where to start or what to do with this.
Help please? Thanks.

Here's my code:
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32818

Gists are great for longer code. For something this short, it is ok to just post it inline.

The code in the gist is:

As far as how to test it, think about what it does. I see two paths through the code.

Exception case
If you pass null or a file that doesn't exist, you expect the error path to be executed. You could test that an exception isn't thrown to the caller. Or you could test that something is written to System.err if you want to be particularly thorough about it. (To do this, save System.err in an instance variable and change it to a stream you control.)

Happy path
The goal is to test a file is created with certain content. One way is to create a sample serializable object. Run it through your method. Then try to do the opposite - read in the file and see if you get the same object.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: JUnit Testing.
It's not a secret anymore!