Two Laptop Bag*
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
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: junit 4 - test suite annotations
 
Similar Threads
noobie junit issue
JUnit 3.8: trying to accomplish something like Parameterized J-Unit 4.0
maven not running my testfile in Selenium in command line
junit program
how to design a test where big files involved ?