• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

How to test a load and save error?

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently I am working on a JUnit Test class for one of my projects and need to test every throw and true or false and so on. I have a load and save method that works correctly but I need to get the method to not work and show that in a test method.

Here's my load and save methods,


My test case is this


But like I said I am trying to get 100 percent coverage in my code and need to get the save and load methods to run this line to prove that if it is actually wrong it will say something is wrong.


I have tried this but It does not work so what am I doing wrong?

 
Rancher
Posts: 4271
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "it does not work"?

The only way I can see to get that method to fail is to have the test attempt to write to a file it is not allowed to write to.
That might be a slightly fragile test (it's reliant on a specific set up of file and user), but it's the only way that comes to mind at the moment.

Edit: That's for the save method.
For the load method it should throw a FileNotFoundException if the file does not exist, which I would expect to be the case here?
 
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That's not how you tell JUnit to expect an exception to be thrown. Search for JUnit @Test expected
 
Sheriff
Posts: 6268
167
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how one might test for an exception:
 
We're all out of roofs. But we still have tiny ads:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!