This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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

 
Ranch Hand
Posts: 66
  • 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: 3887
45
  • 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: 9710
192
  • 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: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about that, same but it takes integes.
 
Dave Tolls
Master Rancher
Posts: 3887
45
  • 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: 66
  • 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: 3887
45
  • 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: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something like this?
 
must Janik
Ranch Hand
Posts: 66
  • 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: 9710
192
  • 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: 9710
192
  • 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: 66
  • 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: 9710
192
  • 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: 66
  • 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: 66
  • 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: 9710
192
  • 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: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should I change library?
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!