aspose file tools*
The moose likes Beginning Java and the fly likes [Agile Java] Any Java 5.0-specific testing idioms/practices? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[Agile Java] Any Java 5.0-specific testing idioms/practices?" Watch "[Agile Java] Any Java 5.0-specific testing idioms/practices?" New topic
Author

[Agile Java] Any Java 5.0-specific testing idioms/practices?

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Hi Jeff,

Welcome!

Do you have any programming idioms or practices geared towards testing or taking advantage of the new language features introduced in Java 5.0 like generics, autoboxing/unboxing, and annotations?


Junilu - [How to Ask Questions] [How to Answer Questions]
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
One of the straplines on the back cover is:
Offers seamlessly-integrated explanations of Java 5.0's key innovations, from generics to annotations
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
Greetings Junilu,

Rather than relegate them all to a separate chapter, the new J2SE 5.0 language features are part of the natural evolution of how you'll learn Java. Throughout Agile Java, I do weigh in on the appropriateness of various Java language features. This includes the 5.0 features. I demonstrate best practices for using each, including how to use them in tests.

It ended up being a natural fit. What I found is that many of the new 5.0 features greatly simplify your initial learning curve. Tougher-to-understand concepts, such as casting and iterators, can now wait until later in the book.

Early on in Agile Java, you'll learn how to take advantage of things like generics, the enhanced-for loop, and annotations from a "users" standpoint. Later, I teach you how to master these concepts and create your own parameterized (genericized) types, your own iterable classes (so that other code can use the for loop against them), and your own custom annotations. All along the way, I demonstrate and discuss preferred use of these features.

Regards,
Jeff


Books: Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Jeff, that sounds great!

I often read/hear about complains that the "syntactic sugar" introduced with Tiger unncessarily complicates Java. I always felt that things like generics, enhanced for loop, enums etc. actually will simplify my code, but wasn't sure about the appropriateness for beginners. What you write seems to make much sense, though - and is a great relief to me...
[ March 09, 2005: Message edited by: Ilja Preuss ]

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
Just about all of the new features in 5.0 are about simplification of effort and/or conceptual understanding.

I'm disappointed only with the implementation of generics, but there are reasons that Sun did it the way they did. And for a beginning developer, they don't need to worry about building their own parameterized types until much later. Using them is pretty straightforward and adds a useful safeguard (but as with all safeguards, their value diminishes somewhat with good testing).

-Jeff-
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Originally posted by Jeff Langr:
I teach you how to master these concepts and create your own parameterized (genericized) types, your own iterable classes (so that other code can use the for loop against them), and your own custom annotations. All along the way, I demonstrate and discuss preferred use of these features.


Since you mentioned that you were using the TDD approach throughout the book, I was wondering more on the lines of whether there were any new idioms related to doing TDD to develop code that used new features in Java 5.0. Say for example, I wanted to write a class that had methods that used generics; are there any differences in how I would do TDD for this class? Or say annotations. Is there a way to test-drive annotations?
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
Originally posted by Junilu Lacar:
Since you mentioned that you were using the TDD approach throughout the book, I was wondering more on the lines of whether there were any new idioms related to doing TDD to develop code that used new features in Java 5.0. Say for example, I wanted to write a class that had methods that used generics; are there any differences in how I would do TDD for this class? Or say annotations. Is there a way to test-drive annotations?


Greetings Junilu,

Sorry I didn't answer that better. To directly answer your last question, yes, you can test-drive annotations. I include a half-chapter worth about annotations, in which you incrementally develop your own simple testing tool (like JUnit but without a GUI). As far as idioms, I'm not sure I'd call this an idiom, but the way you test annotations is to provide dummy classes with methods or other elements that you annotate. The easiest way is to supply an additional package-level class that does nothing; I put it directly in the same source as the test class itself.

Here's a bit of paraphrased test code that demonstrates this:



(Note that there's some bootstrapping necessary here...)

In other words, I supply the class SingleMethodTest solely for the purpose of demonstrating the @TestMethod annotation. One might call this a mock. I won't, since the term "mock" carries specific connotations for certain people. The benefit of using such dummy classes, as opposed to classes actually in your system, is that they are stable, since they're only used by the test.

For testing parameterized types, the way you approach testing their functionality doesn't change. What does change is that you need to ensure that the behavior isn't adversely impacted when you change what you bind the parameterized type to. Other than trying to test at least two possibilities for each bind parameter, I haven't developed an idiom here.

In general, the new language features in 5.0 don't alter your fundamental approaches to testing: decide how you want things to work from a client perspective, and then figure out how you're going to prove that things actually worked that way. Since Agile Java goes through all the new language features, you'll see one or more ways to approach testing each of them.

Regards,
Jeff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Agile Java] Any Java 5.0-specific testing idioms/practices?