aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about assertion - appropriate usage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about assertion - appropriate usage" Watch "question about assertion - appropriate usage" New topic
Author

question about assertion - appropriate usage

Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 146

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


Adolfo Eloy
Software Developer
OCPJP 6
Elier Rguez
Greenhorn

Joined: Jun 27, 2013
Posts: 22
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

Joined: Dec 11, 2012
Posts: 46
    
    1

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


Twitter:ben_pheonix
Elier Rguez
Greenhorn

Joined: Jun 27, 2013
Posts: 22
Thanks for your complete answer. Doubt cleared up!
Ben Pheonix
Ranch Hand

Joined: Dec 11, 2012
Posts: 46
    
    1

Glad to help.

BR,
Ben
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5


The example above is a more appropriate assertion for the test2 method.
Elier Rguez
Greenhorn

Joined: Jun 27, 2013
Posts: 22
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
 
subject: question about assertion - appropriate usage