Win a copy of Spring in Action (5th edition) this week in the Spring 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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: 21465
97
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.
 
Sheriff
Posts: 4572
286
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.
 
Bartender
Posts: 9494
184
  • 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: 19988
95
Android Eclipse IDE Linux
  • 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.
 
Marshal
Posts: 6263
420
BSD
  • 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
Sheriff
Posts: 4572
286
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!