Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to test a load and save error?

 
Ranch Hand
Posts: 51
  • 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: 4610
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?
 
Sheriff
Posts: 15754
264
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:
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic