This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to unit test method that contains try/catch blocks

 
Ranch Hand
Posts: 99
1
  • 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.
 
Rancher
Posts: 4174
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: 10404
223
  • 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: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about that, same but it takes integes.
 
Dave Tolls
Rancher
Posts: 4174
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: 99
1
  • 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
Rancher
Posts: 4174
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: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something like this?
 
must Janik
Ranch Hand
Posts: 99
1
  • 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: 10404
223
  • 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: 10404
223
  • 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: 99
1
  • 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: 10404
223
  • 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: 99
1
  • 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: 99
1
  • 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: 10404
223
  • 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: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should I change library?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!