InvalidTestClassError - No runnable methods

Recently I encountered an interesting error while running a unit test. This test was passing from my Intellij IDE, but was failing from the maven. Below is the error I got from maven
[ERROR] com.test.SampleTest.initializationError  Time elapsed: 0 s  <<< ERROR!
org.junit.runners.model.InvalidTestClassError: 
Invalid test class 'com.test.SampleTest':
  1. No runnable methods
The error was coming because my test class has following
  • extending BaseTest that extends the junit's TestCase.
  • running my test using @Test from org.testng.annotations.
  • using @RunWith(JMockit.class)
 
@RunWith(JMockit.class)
class SampleTest extends BaseTest {

  @BeforeClass // testng
  public void setUp(){
    super.setUp();
  }
  ... then test cases
}

If I run the test without JMockit removes the No runnable methods error. But it causes few other errors as my test was relying on mocked data. So I removed all the testng @Test and replaced it with junit @Test.

Then it raised another issue, BaseTest class already has a setUp() method which is not static, and I can't use it from a static method (@BeforeClass for setUp()). So I pushed this super.setUp() inside the @Before which runs every time the test runs. But I added a small trick to load it only once.

@RunWith(JMockit.class)
class SampleTest extends BaseTest {

  private boolean initialised = false;
    
  @Before // junit
  public void setUp(){
    if(!initialised){
      super.setUp();
      initialised = true;
    }
  }
  ... then test cases
} 
Another thing I tried was adding @Ignore for the BaseTest so that junit avoids it. But that didn't work for me.

I was using the following command to run it from maven
mvn verify -Dtest=SampleTest -DfailIfNoTests=false

Comments