ChatGPT解决这个技术问题 Extra ChatGPT

JUnit: how to avoid "no runnable methods" in test utils classes

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?

Does your tests work in Eclipse/NetBeans/your favourite IDE?
I use eclipse. Actually there is no problem there, somehow eclipse doesn't try to run these classes. I wonder how?
I don't know if we understood your question. Please re-read your question and probably add some more information.
@guerda: The question seems pretty clear to me. His Ant task is finding classes which don't contain tests, because the filter is picking up the utility class. Hence my answer, which I still believe is entirely relevant.
LiorH: Thanks for clarification, so my answer is waste :)

J
JoelPM

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


Actually, no, it shouldn't. @Ignore is for temporarily disabling tests.
Sorry but that's a bad idea. You want to start annotating your production code with test related annotations just because they might match a test pattern? The proper answer is to fix the class names if they are triggering the pattern matching for tests. And make sure the pattern finds only classes that END with Test. That's a commonly accepted pattern
Yeah, this is bad and I didn't realize it until after contributing another upvote that i can't remove. Make your base class abstract, then JUnit will ignore it. See @gmoore's answer below.
g
gmoore

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

Much better than @Ignore
I tried the @Ignore approach and thought, well that's good, then I read this answer and slapped myself in the forehead, "Of course!"
J
Jon Skeet

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.


I don't think it would help, because he moved to JUnit 4.4 and that should not matter.
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.
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)
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.
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.
f
froh42

To prevent JUnit from instantiating your test base class just make it

public abstract class MyTestBaseClass { ... whatever... }

(@Ignore reports it as ignored which I reserve for temporarily ignored tests.)


JUnit runners often try to instantiate abstract classes as well, and then fail with an instantiation error.
Works perfectly for my for base test classes
This is working because of the name (it doesn't end in Test), not because of the abstract modifier. Change the class name to MyBaseClassTest and it will try to instantiate as mentioned by @HollyCummins (and fail)
In my case, it should be protected abstract class.
R
Raghu K Nair

If this is your base test class for example AbstractTest and all your tests extends this then define this class as abstract If it is Util class then better remove *Test from the class rename it is MyTestUtil or Utils etc.


S
Sridhar Sarnobat

Be careful when using an IDE's code-completion to add the import for @Test.

It has to be import org.junit.Test and not import org.testng.annotations.Test, for example. If you do the latter, you'll get the "no runnable methods" error.


This should be a comment rather than an answer.
I don't see why. It's a valid solution.
Intellij Idea 2017 was messing with my mind by importing org.junit.jupiter.api.Test instead! but thanks to you it is solved now
Thank you very much, I was confused during getting problem "no runnable methods".
m
mc1arke

Ant now comes with the skipNonTests attribute which was designed to do exactly what you seem to be looking for. No need to change your base classes to abstract or add annotations to them.


It looks like the skipNonTests attribute is only available in ant 1.9+, which is a shame, since it looks incredibly useful. It will also exclude abstract test superclasses.
a
akuhn

What about adding an empty test method to these classes?

public void avoidAnnoyingErrorMessageWhenRunningTestsInAnt() {
    assertTrue(true); // do nothing;
}

but that falsely increases the number the tests we have :) not that its a big deal
i
illuminatos

In your test class if wrote import org.junit.jupiter.api.Test; delete it and write import org.junit.Test; In this case it worked me as well.


amazingly, it worked. i executed manually in Windows command line. however, another problem is that @BeforeAll and @AfterAll are not run.
seemingly, JUnit4 worked (with @BeforeClass and @AfterClass, but JUnit5's not. Reference: junit.org/junit5/docs/current/user-guide/#migrating-from-junit4
R
Rahul Vig

I was also facing a similar issue ("no runnable methods..") on running the simplest of simple piece of code (Using @Test, @Before etc.) and found the solution nowhere. I was using Junit4 and Eclipse SDK version 4.1.2. Resolved my problem by using the latest Eclipse SDK 4.2.2. I hope this helps people who are struggling with a somewhat similar issue.