• 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to test a load and save error?

 
Ranch Hand
Posts: 56
  • 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?

 
Master Rancher
Posts: 4700
49
  • 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?
 
Sheriff
Posts: 15936
265
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: 7108
184
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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic