• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the straplines on the back cover is:

Offers seamlessly-integrated explanations of Java 5.0's key innovations, from generics to annotations

 
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Jeff Langr
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic