This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source 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
  • 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

JUnit Assert that a value is in fact an Int  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm passing a string into an assertStatement, and want to assert that the value passed in as a String, is in fact an integer (after converting it to an integer).

How do I do this using a JUnit assert?
 
Sheriff
Posts: 21741
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try using assertThat with a (probably custom) matcher. If you extend TypeSafeMatcher or TypeSafeDiagnosingMatcher it won't be too hard.
 
Marshal
Posts: 4622
299
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any exception thrown from a JUnit method will cause that test to fail. So you could simply attempt to convert it to an Integer.

Alternatively if you wanted a custom failure message you could catch the exception and explicitly fail the test with your message.
 
Saloon Keeper
Posts: 10206
216
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make it explicit in the way Rob mentioned. If you don't mind boxing, you could do this:
 
Bartender
Posts: 20721
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, mixing "live" logic and an assertion is bad practice.

In the C language, which is where the original "assert" concept comes from, the assertion mechanism wasn't part of the language spec, it was done using the C precompiler. You'd do something like this:


It was, in fact, intended to be able to load up a program with lots of testing code that would effectively not exist when assertions were turned off, so that when you went to production mode, you wouldn't be burdened with a ton of overhead for code whose sole purpose to ensure that supposedly good data and program states really were good data and program states.

The phrase "assert that the value passed in as a String, is in fact an integer (after converting it to an integer)." is morally dubious anyway. If you convert a String to an integer, of course the result is going to be an integer. It's in the very definition of the operation. In actuality, what you are saying is that you want to assert that the conversion has succeeded (since you used the word "after"). And for that, an assertion is a bad way of doing things, since, to repeat what I said initially, an assertion is something that effectively shouldn't exist when running in production.

So the closest thing to the stated goal would be Tim Cooke's second code example. Except that since in effect, it's swallowing an Exception when running in production code, it violates one of my most sacred programming principles, which is to never simply swallow an Exception.
 
Sheriff
Posts: 6738
466
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark D Anthony wrote:I'm passing a string into an assertStatement, and want to assert that the value passed in as a String, is in fact an integer (after converting it to an integer).



@OP

Pay attention to what are you actually testing, whether the string conversion resulted in an integer, or whether the string represents an actual integer?
If the latter, you need to test not after the conversion takes place, but also before the conversion, so you could control it and not rely on somebody else (if that's possible).

An example:

Test:
Test 1 passed; Test 2 failed; Test 3 passed;
 
Tim Cooke
Marshal
Posts: 4622
299
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps Mark (the OP) can provide some more information with a code example of what's he's working with? It'll help us give better advice.

In general terms this smells like a bad design where integer values are being represented with the String type, when they would be better represented with the int or Integer type.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!