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

后端 未结 10 683
清酒与你
清酒与你 2020-11-30 05:35

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 patt

相关标签:
10条回答
  • 2020-11-30 05:41

    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.

    0 讨论(0)
  • 2020-11-30 05:50
    1. If this is your base test class for example AbstractTest and all your tests extends this then define this class as abstract
    2. If it is Util class then better remove *Test from the class rename it is MyTestUtil or Utils etc.
    0 讨论(0)
  • 2020-11-30 05:55

    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.

    0 讨论(0)
  • 2020-11-30 05:58

    What about adding an empty test method to these classes?

    public void avoidAnnoyingErrorMessageWhenRunningTestsInAnt() {
        assertTrue(true); // do nothing;
    }
    
    0 讨论(0)
  • 2020-11-30 05:59

    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
    
    0 讨论(0)
  • 2020-11-30 06:03

    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.

    0 讨论(0)
提交回复
热议问题