• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JUnit-what to test?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic