4

I was recently working with JUnit 3, but I decided to migrate to JUnit 4. Now Im facing the following problem:

I was using a TestSuite with JUnit 3, where I ran all java-Testclasses whose names matched a pattern like "*TestMe.java".

I've got a hundred tests which were named like this.

Now, with JUnit 4 I have to name them explicitly to call them in a TestSuite.

@RunWith(Suite.class)
@Suite.SuiteClasses(
{
    FirstTestMe.class,
    SecondTestMe.class,
    ThirdTestMe.class,

})
public class TestMe
{
    /**
     * Constructor.
     */
    private TestMe()
    {
        super();
    }
}

This is really uncomfortable and I possibly might forget to list some tests. Also, when I create a new one, I must add it there.

Is there any solution how to call those Test-Classes with a Regex or something else?

Also, one additional question: each method which is not a test, but maybe used in a test-class must be annotated with @Ignore? I run those test-classes without any error, so I guess it is not necessary?

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
nano7
  • 2,455
  • 7
  • 35
  • 52

4 Answers4

1

You may want to have a look at the post migrate-tests-from-junit-3-to-junit-4 which discusses exactly what you want.

See if Dynamically create a Test Suite in JUnit 4 or Run all tests in Junit 4 helps.

Community
  • 1
  • 1
Kuldeep Jain
  • 8,409
  • 8
  • 48
  • 73
  • I just looked at it, but I didnt find the specific answer to this question. But thanks anyway. – nano7 Feb 14 '12 at 08:49
0

Add the @Test annotation above your test method.

public class TestMe
{
    @Before
    public void setUp() {

    }

    @Test
    public void myFirstTest() {
    }

}
Paul Nikonowicz
  • 3,883
  • 21
  • 39
  • This is just how to write a simple Test-Class. Im looking for a way how to call multiple TestClasses in one Suite. I want to be able to run a specific selection of TestClasses. But thanks for your answer. – nano7 Feb 14 '12 at 08:48
0

For your first question, if you want to use a Test Suite in JUnit 4, then there isn't any option but to list all of the classes explicitly. If, however, you want to run all classes in a package, or in a project, you can just right click on the package/project in Eclipse and select Run as JUnit test. If you're using ant or maven surefire, you can specify the tests to run with a *Test.java or similar.

You don't have to annotate those methods which are not test methods with @Ignore. The (major) difference between JUnit 3 and JUnit 4 is the use of annotations. So it JUnit3, you extend TestCase and name all of your methods testXXX. In JUnit 4, you don't have to extend TestCase, but you do have to annotate all of your test methods with @Test. So therefore, any methods not marked with @Test don't get run as test. Of course, there are other annotations such as @Before and @After, which replace setUp() and tearDown().

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • Ok, so there is no way to run a selection of TestClasses expect for specifying a profile in maven? I know how to run all tests, but I want to run just a selection. Thanks for your answer! – nano7 Feb 14 '12 at 08:50
0

There is an open source library ClasspathSuite. It allows you to use regular expressions to specify what class/package names should be or should not be included in your test suite.

Here's an example of use which includes everything in two packages but not in a third package. It also excludes another specific class.

@ClassnameFilters( { "com.javaranch.*test.*", "net.jforum.*test.*", "!com.javaranch.test.web.*", "!.*All_JForum_Functional_Tests" })
Jeanne Boyarsky
  • 12,156
  • 2
  • 49
  • 59