views:

3937

answers:

4

I have switched to JUnit4.4 from JUnit3.8. I run my tests using ant, all my tests run successfully but test utility classes fail with "No runnable methods" error. The pattern I am using is to include all classes with name *Test* under test folder.

I understand that the runner can't find any method annotated with @Test attribute. But they don't contain such annotation because these classes are not tests. Surprisingly when running these tests in eclipse, it doesn't complain about these classes.

In JUnit3.8 it wasn't a problem at all since these utility classes didn't extend TestCase so the runner didn't try to execute them.

I know I can exclude these specific classes in the junit target in ant script. But I don't want to change the build file upon every new utility class I add. I can also rename the classes (but giving good names to classes was always my weakest talent :-) )

Is there any elegant solution for this problem?

+2  A: 

Assuming you're in control of the pattern used to find test classes, I'd suggest changing it to match *Test rather than *Test*. That way TestHelper won't get matched, but FooTest will.

Jon Skeet
I don't think it would help, because he moved to JUnit 4.4 and that should not matter.
furtelwart
You seem to have missed the point of my answer. He has a name filter to determine the classes to be considered as tests. If he changes the filter, he can easily exclude the helper classes.
Jon Skeet
Your suggestion is valid, however I checked out my tests classes and some start with Test and some end with Test. no clear distinction between utility classes and real test classes. Do you think the convention you suggested is a good practice? (i.e. utils start with Test, and tests end with Test)
LiorH
It's almost a convention that you suffix the testcase classes with *Test. You might need to refactor by renaming test classes appropriately and also rename the helpers so they won't use that suffix convention.
Spoike
I agree with Spoike - if you can't tell from the name of the class whether it's a test or a helper, you should rename the class. The convention is more "the class is a test if and only if it ends with Test." Utility classes may or may not begin with Test - it doesn't matter.
Jon Skeet
we'll go with the convention. Thanks
LiorH
@Downvoter: Care to comment?
Jon Skeet
+1  A: 

What about adding an empty test method to these classes?

public void avoidAnnoyingErrorMessageWhenRunningTestsInAnt() {
    assertTrue(true); // do nothing;
}
Adrian
interesting, thanks
LiorH
+6  A: 

Annotate your util classes with @Ignore. This will cause JUnit not to try and run them as tests.

JoelPM
A: 

My specific case has the following scenario. Our tests

public class VenueResourceContainerTest extends BaseTixContainerTest

all extend

BaseTixContainerTest

and JUnit was trying to run BaseTixContainerTest. Poor BaseTixContainerTest was just trying to setup the container, setup the client, order some pizza and relax... man.

As mentioned previously, you can annotate the class with

@Ignore

But that caused JUnit to report that test as skipped (as opposed to completely ignored).

Tests run: 4, Failures: 0, Errors: 0, Skipped: 1

That kind of irritated me.

So I made BaseTixContainerTest abstract, and now JUnit truly ignores it.

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
gmoore