Super JUnit classes without test cases

I have three JUnit test classes that have common code, including identical setup () methods. So, I legalized this code in my class, which extends TestCase and now has the three previous JUnit test classes that extend this new class. The new superclass does not contain tests.

However, in our build, JUnit runs all of the JUnit test classes, including the new superclass without tests. He gives this error:

junit.framework.AssertionFailedError: No tests found in com.acme.ControllerTest 

I could get rid of this error by creating a simple test that does nothing in ControllerTest. But is there a cleaner way to fix this?

+4
source share
1 answer

It works for me

Junit 3

 public abstract class BaseTest extends TestCase { public void setUp(){ System.out.println("before called"); } } public class Test1 extends BaseTest { public void test() { Assert.assertTrue(true); System.out.println("Test1"); } } public class Test2 extends BaseTest { public void test() { Assert.assertTrue(true); System.out.println("Test1"); } } 

The output I get is

 before called Test2 before called Test1 

Junit 4

For JUnit4, you don’t even need to abstract from the base class. You can simply use the following

 public class BaseTest { @Before public void setUp(){ System.out.println("before called"); } } public class Test1 extends BaseTest { @Test public void test() { Assert.assertTrue(true); System.out.println("Test1"); } } public class Test2 extends BaseTest { @Test public void test() { Assert.assertTrue(true); System.out.println("Test1"); } } 

I would highly recommend using JUnit 4. Using annotations means you break some of these inheritance dependencies, which can be confusing.

+7
source

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


All Articles