Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run all my inner class junit tests at once

I have organised my junit tests using one inner class per method as described:

Here, in a haacked article (about nunit testing), and
Here, in this SO question

public class TestMyThing {

    public static class TestMyThingMethodOne {

        @Test
        public void testMethodOneDoesACertainBehaviour() {
            // terse, clear testing code goes here
        }

        @Test
        public void testMethodOneHasSomeOtherDesiredBehaviour() {
            // more inspiring testing code here
        }
    }

    public static class TestMyThingMethodTwo {
        ... etc
    }
}

However, when I try to run the junit tests for this, Eclipse asks me which of the inner class tests I want to run and I can only choose one. Is there a way I can specify that I want every test in every inner class for the TestMyThing class to run?

like image 721
Joe Avatar asked Jan 31 '12 10:01

Joe


2 Answers

I asked my question on the Eclipse forum in this post and it turns out that it is possible for Eclipse to run all the inner classes. I quote the reply, which I've tried and can confirm that it works great (i.e. I can call all of the tests in my inner classes with one command):

JUnit 4 comes with a runner class called Enclosed, which does what you need.

Annotate the outer class with @RunWith(Enclosed.class) with Enclosed = import org.junit.experimental.runners.Enclosed;

like image 157
Joe Avatar answered Oct 17 '22 06:10

Joe


Usually you would just create separate classes ( just pojos with default constructor ) instead of packing distinct unit tests into single class. As your inner classes are static, there is no real advantage in doing this.

like image 22
Konstantin Pribluda Avatar answered Oct 17 '22 05:10

Konstantin Pribluda