This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

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: 21694
101
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: 4600
295
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: 9997
208
  • 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: 20580
121
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: 6634
443
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
Sheriff
Posts: 4600
295
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.
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!