• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

How to unit test method that contains try/catch blocks  RSS feed

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gotta to test method that is responsible for taking input.
It contains try/catch blocks. Should I unit test? If so, how to do it?

   

I tried mocking Scanner, but it is impossible to mock final classes.
 
Master Rancher
Posts: 4072
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can "mock" Scanner by providing a pre-canned String for it to work off.

However, next() does not ever throw an InputMismatchException, so you can't force that catch block to occur anyway.
 
Saloon Keeper
Posts: 10206
216
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You first need to give a better example, because your code is functionally completely equivalent to:

The loop is useless and input.next() will never throw an InputMismatchException.
 
must Janik
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about that, same but it takes integes.
 
Dave Tolls
Master Rancher
Posts: 4072
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That at least makes a bit of sense.
Assuming you are happy that the user will be forced to CTRL-C to exit the loop.
 
must Janik
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is that? Everything works fine. If user put bad value, program asks about one more value. If user put good value, program goes on.
 
Dave Tolls
Master Rancher
Posts: 4072
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if user decides they don't want to keep entering values, and want to go back to whatever the previous step was?
That's what I meant (though I wasn't terribly clear).

But that's not terribly important in any case.

This method can be tested using the pre-canned String that I suggested earlier.
 
must Janik
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something like this?
 
must Janik
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, how to test `catch`?
I think that is impossible to as I did.
 
Stephan van Hulst
Saloon Keeper
Posts: 10206
216
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can very nicely test this with JUnit 4's experimental Theories library:
 
Stephan van Hulst
Saloon Keeper
Posts: 10206
216
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

must Janik wrote:Actually, how to test `catch`?


Like this:
 
must Janik
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well.. I don't know what exactly it is and how it works. I would rather "primitive" solutions
 
Stephan van Hulst
Saloon Keeper
Posts: 10206
216
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

must Janik wrote:Well.. I don't know what exactly it is and how it works. I would rather "primitive" solutions


Try out the simpler solution in my previous post.
 
must Janik
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

must Janik wrote:Well.. I don't know what exactly it is and how it works. I would rather "primitive" solutions


Try out the simpler solution in my previous post.



Thanks, Stephan!
 
must Janik
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I wanted to do same for Long numbers, but
this libraries doesn't operate on long numbers.
I did something like this. Really similiar, can be?
 
Stephan van Hulst
Saloon Keeper
Posts: 10206
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It certainly should work with long values, but you need to use long literals and not int literals. By the way, don't use org.hamcrest.CoreMatchers, use org.hamcrest.Matchers instead.
 
must Janik
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should I change library?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!