File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Testing and the fly likes JUnit-what to test? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "JUnit-what to test?" Watch "JUnit-what to test?" New topic

JUnit-what to test?

karina val

Joined: Sep 02, 2011
Posts: 6
I have to write JUnit test cases for my class CheckVul (code below) but it's my first time using JUnit and am stuck on how to make test cases for these methods since they all are void, and write to a file. The checkDatabase method takes in a 'profile name' and a 'year' provided by the user, to compare that profile with the database of that year and check for vulnerabilities. I have a basic understanding of how to write test cases for simple methods that return something, etc. but not for this. Any suggestions? :S I was thinking making a test case for the checkDatabase method would be simple, but not sure how to go about it.

Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

If I were the one being asked to test this, I would first have a serious talk with the developer about the design.

First, it's not unit testable since it expects input from a file. A unit test should not cross system boundaries, that is, it should not access the file system, a database, a web service, etc. For this class, you can only write integration tests, which are expected to cross system boundaries.

Second, the design violates the Open-Closed Principle. What happens in 2013? In 2014? In 2015? etc. Seems like you'd need to change this code every year. That's not a good design.

One other nitpick (my pet peeve): why skimp on keystrokes and make the class name less readable? What's wrong with naming the class VulnerabilityChecker or something that is easy to read without having to do a mental expansion? Write code with the reader in mind. Don't skimp on keystrokes this way. BTW, a class name should generally be a noun or noun phrase. CheckVul, assuming "Vul" is a lazy short form of "Vulnerability", is a verb phrase and is more suitable as a method name.

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

Joined: Feb 26, 2001
Posts: 6529

Another thing, why are the checkVul**() methods public? Shouldn't these be private instead? It looks like the checkDatabase() method is meant to be called and then it will call the appropriate checkVul**() method. Since the checkVul** methods are public, there's nothing that prevents them from being called with the wrong parameter values. If they are meant to be public, then why have the checkDatabase() method at all?
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33107

You could have the unit test read the file to assert what was written out. Or you could refactor GenerateReport to provide a way to write to a variable rather than a file.

[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-what to test?
It's not a secret anymore!