Migration: JUnit 3 - JUnit 4: TestSuite

I recently worked with JUnit 3, but decided to upgrade to JUnit 4. Im now facing the following problem:

I used TestSuite with JUnit 3, where I ran all java-Testclasses whose names corresponded to a pattern like "* TestMe.java".

I have one hundred tests that have been named like this.

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

@RunWith(Suite.class) @Suite.SuiteClasses( { FirstTestMe.class, SecondTestMe.class, ThirdTestMe.class, }) public class TestMe { /** * Constructor. */ private TestMe() { super(); } } 

This is really inconvenient, and I may have forgotten to list some tests. Also, when I create a new one, I have to add it there.

Is there any solution how to call these test classes using Regex or something else?

Also, another question: should every method that is not a test, but can be used in a test class, be annotated using @Ignore? I run these test classes without any error, so I think this is not necessary?

+4
source share
4 answers
+1
source

Add a @Test note above your test method.

 public class TestMe { @Before public void setUp() { } @Test public void myFirstTest() { } } 
0
source

For your first question, if you want to use the test package in JUnit 4, then there is no option, but I explicitly list all the classes. However, if you want to run all classes in a package or project, you can simply right-click on the package / project in Eclipse and select Run as JUnit test . If you use ant or maven surefire, you can specify the tests to run using * Test.java or the like.

You do not need to comment on methods that are not testing methods using @Ignore . The main difference between JUnit 3 and JUnit 4 is the use of annotations. So, JUnit3, you extend TestCase and call all your testXXX methods. In JUnit 4, you do not need to extend TestCase, but you need to annotate all your test methods with @Test . Therefore, any methods that are not marked with @Test do not run as a test. Of course, there are other annotations like @Before and @After that replace setUp() and tearDown() .

0
source

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

Here is a usage example that includes everything in two packages, but not in the third package. It also excludes another specific class.

 @ClassnameFilters( { "com.javaranch.*test.*", "net.jforum.*test.*", "!com.javaranch.test.web.*", "!.*All_JForum_Functional_Tests" }) 
0
source

Source: https://habr.com/ru/post/1396261/


All Articles