This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Testing and the fly likes junit 4 - test suite annotations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "junit 4 - test suite annotations" Watch "junit 4 - test suite annotations" New topic
Author

junit 4 - test suite annotations

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

In JUnit 3.8, you could create your own suite dynamically be implementing a suite() method. In JUnit 4.0, you do something like:
@SuiteClasses( { A.class, B.class }).

Is there a way to build this list dynamically? (This may be more about annotations in which case, we can move it to Java In General.) I'd like to be able to build a test suite to include all the test classes it finds in the classpath excluding certain packages. In JUnit 3.8, I implemented this logic in the suite() method.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

Bumping this because it's been nine months and state of the art may have changed. That and the increased traffic due to the promotion this week.

Since I posted this in December, I learned that we can't pass a method to the annotation so it isn't possible in an annotation based way. I'm thinking one solution is to write a custom RunWith implementation. Any other ideas? Or comments on this approach.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Jeanne, a custom runner is "The" way to extend JUnit 4 in ways such as the one you describe. However, you should probably first look into whether Johannes Link's ClasspathSuite does the trick for you.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

Lasse,
Yes! That does what I need. Now just need to patiently wait until we can actually use Java 5.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Note that you can also use the old suite()-Method style with JUnit 4 by using the AllTests runner.


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
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

Originally posted by Lasse Koskela:
Jeanne, a custom runner is "The" way to extend JUnit 4 in ways such as the one you describe. However, you should probably first look into whether Johannes Link's ClasspathSuite does the trick for you.

It does now. It almost did what I needed - the only thing missing was the ability to include JUnit 3.X tests too and run a mix. Johannes included it in the latest version less than a week after I suggested it and provided a possible implementation.

ClasspathSuite is great! So is open source for that matter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: junit 4 - test suite annotations