Super class and abstract test method with JUnit5 tests

377 Views Asked by At

I migrated unit tests from JUnit4 to JUnit5 and I use Intellij as IDE
With JUnit4, I had some tests implementing two methodes of a test superclass. It seems like I could put the @Test on the abstract methods and just override them in subclasses.

import org.junit.jupiter.api.Test;

    @SpringBootTest(classes = {TestContext.class})
    public abstract class AbstractTest {
    
        @Test
        public abstract void testOk();
    
        @Test
        public abstract void testKo();
    }

And the test

    public class SomeTest extends AbstractTest {
    
        // @Test with JUnit5
        @Override
        public void testOk() {
            //
        }
    
        // @Test with JUnit5
        @Override
        public void testKo() {
            //
        }
    }

But with JUnit5, it seems like I have to put @Test annotation on the overriding method of the subclass SomeTest, otherwise tests are not found. Is there a way to achieve my previous configuration with JUnit5, avoiding to add @Test to all subclasses methods ?

1

There are 1 best solutions below

0
TCH On BEST ANSWER

Following M. Deinum recommandation, I ended up with this

import org.junit.jupiter.api.Test;

@SpringBootTest(classes = {TestContext.class})
public abstract class AbstractTest {
    
   public abstract void testOk();
    
   public abstract void testKo();

   @Test
   public void junitTestOk() {
       this.testOk();
   }
    
   @Test
   public void junitTestKo() {
       this.testKo();
   }
}