1

In JUnit3, one would could name a test suite like this:

public static Test suite() {
    TestSuite suite = new TestSuite("Some test collection");
    suite.addTestSuite(TestX.class);
    return suite;
}

Is there an equivalent way to do this in JUnit4?

Thanks.

EDIT

Thank you, I actually managed to get it working. My question was if there is a JUnit4 equivalent way of specifying the name/description of a test suite, like in JUnit3 with "Some test collection".

Some background: I'm converting junit tests in legacy code to the version 4, and I don't want to lose any information if possible. I apologize, I should really have been more specific in the original question.

lv.
  • 446
  • 6
  • 18
  • Isn't [`@RunWith(Suite.class)`](http://junit.sourceforge.net/javadoc/org/junit/runners/Suite.html) meant for exactly this purpose? – blgt Jan 12 '16 at 13:48
  • Yes, it's what I used. I just didn't find anyway to set the test suite name to a default string, like in JUnit 3. I've edited the question since in retrospective I was indeed overly vague. – lv. Jan 12 '16 at 14:17

2 Answers2

2

You can do this with the Suite runner @RunWith(Suite.class):

@RunWith(Suite.class)
@SuiteClasses({Test1.class, Test2.class, TestX.class})
public class MySuite {}

Where Test1, Test2, TestX contain your tests

ref. RunWith, Suite

update:

WRT changing the actual description of your suite, I don't think there's a way to do it out-of-the-box (if there is I haven't seen it yet). What you can do, is to define your own runner with a custom description [update2]:

@RunWith(DescribedSuiteRunner.class)
@SuiteClasses({Test1.class, Test2.class, TestX.class})
@SuiteDescription("Some test collection")
public class MySuite {}

public class DescribedSuiteRunner extends Suite {
    // forward to Suite
    public DescribedSuiteRunner(Class<?> klass, RunnerBuilder builder)
            throws InitializationError {
        super(klass, builder);
    }

    @Override
    protected String getName() {
        return getTestClass()
                .getJavaClass()
                .getAnnotation(SuiteDescription.class)
                .value();
    }
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface SuiteDescription {
    String value();
}

The default implementation of getName just returns the class being tested's name

blgt
  • 8,135
  • 1
  • 25
  • 28
1

Yes, In JUnit 3.x, the JUnit methods had to be specifically named. They needed to begin with the word test in order for JUnit to run that as a test case. Now you can just use the @Test annotation:

@Test
public void thisIsMyTest() {
    // test goes here
}

Also in JUnit4 you can state if you want some tests to run before or after all the tests in this class are invoked:

@Before
public void init() throws Exception {
    System.out.println("Initializing...");
}

@After
public void finish() throws Exception {
    System.out.println("Finishing...");
}

Further comparisons between JUnit3 and JUnit4 here and here.

Edit: after blgt's comment, I see I might have misunderstood your intent. You are probably looking for @RunWith(Suite.class) - When a class is annotated with @RunWith, JUnit will invoke the class in which is annotated so as to run the tests, instead of using the runner built into JUnit. Full example of usage is here, tl;dr below:

@RunWith(Suite.class)
@SuiteClasses({ FirstTest.class, SecondTest.class })
public class AllTests {
    ...
}
Community
  • 1
  • 1
Idos
  • 15,053
  • 14
  • 60
  • 75
  • Thanks, though I what I meant was trying to find a way to name the test suite itself, like in the example. Sorry, I've edited the question above. – lv. Jan 12 '16 at 14:16