Win a copy of Microservices Testing (Live Project) 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Assert doubt

 
Ranch Hand
Posts: 78
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: Software Developer's Journal (01/2010)
Which assertion will compile AND will be done correctly:

d) boolean t;
assert t = false;
e) assert false : true;

I've just put 2 points which in the answer are CORRECT. In my view their're wrong, because we should assert that something is TRUE. Am I right?
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
assert statements come in 2 flavor.
the syntax of assert statement is :
assert (condition);

or

assert (condition): (xxxx);


here condition should be condition that will produce either true or false and (xxxx) can be anything whose string value is printed along with stack trace when assertion condition goes false.

so for first case : assert t = false; means assert false. remember an assignment statement returns the value being assigned , and in this case it returns false. so assert false; is a correct assert statement syntaxically.

the second case : assert false: true; here the assert false is sure to throw an assertion error. the second statement is 'true' which is a boolean literal and its string value is true so true is printed along with stack trace.remember you can use those expressions after the colon : that you can use with System.out.print( ) , that is which returns a string value.
 
reply
    Bookmark Topic Watch Topic
  • New Topic