Joe
Joe

Reputation: 4930

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?

Upvotes: 5

Views: 2557

Answers (2)

Joe
Joe

Reputation: 4930

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;

Upvotes: 8

Konstantin Pribluda
Konstantin Pribluda

Reputation: 12367

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.

Upvotes: 1

Related Questions