File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about assertion - appropriate usage

 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody.

I'd like to understand why the assertion on line 13 isn't appropriate.
The book's answer about which are true is that
neither line 5 and line 13 assertions is appropriate because ultimately they are testing the args of main().

But I think that test2 method could be used with another argument than the main's argument.
Could someone give me some help to understand it? Thanks!!!


From: JAVA OCP Java SE 6 Programmer Practice Exams - K&B
Chapter 3: Practice Exam I - exercise number 44



tks
 
Elier Rguez
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Up!

Hi,
I have the same doubt.

I think 'test2' method follow the rule 'do use assertions to validate arguments to a private method'. So why isn't it appropriate?


Thanks in advance.
 
Ben Pheonix
Ranch Hand
Posts: 46
1
Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Assertions are in theory for testing code invariants, assumptions that must be true in order for the code to complete properly.
The example shown is testing for valid input, which isn't a typical usage for an assertion because it is, generally, user supplied.
Assertions aren't generally used in production code because there is an overhead and it is assumed that situations where the invariants fail
have been caught as coding errors during development and testing. LINE-5 and LINE-13 are trying to validate the input, that's why they are
in-appropriate usage ofassertion. Additionally, your assumption is correct that test2 can be used for some other way also, but currently the
author is talking about context of the example so in this example, the way assertion has been used is in-appropriate.

Correct syntax of assertion is

assert <boolean expression>;


Program is using it in a wrong way.



means: a MUST have a lenght equals 2 else is an error. Please check OracleDocumentation

HTH,
Ben
 
Elier Rguez
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your complete answer. Doubt cleared up!
 
Ben Pheonix
Ranch Hand
Posts: 46
1
Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to help.

BR,
Ben
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The example above is a more appropriate assertion for the test2 method.
 
Elier Rguez
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:
The example above is a more appropriate assertion for the test2 method.


I am not sure, because it's still trying to validate a public input. Isn't it?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic