gbegley
gbegley

Reputation: 2679

No Runnable methods Error From Base Test class

I have a A few base test classes that setup common configurations for spring,logging,jndi etc using test execution listeners that are then inherited by subclasses. This is done so tests can just run code without having to worry about getting jndi and logging services in place before being able to run testing code.

Using intellij and invoking "run all tests" from the project base, the IDE attempts to run the base test class as a unit test and gives me the "No runnable methods" error.

I know I could put an empty runnable method in the base class, but I was hoping some one has a better idea.

The Base class is:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {
            "classpath:spring-jndi.xml"
    })
    @TestExecutionListeners({
            Log4JSetupListener.class,
            JndiSetupListener.class,
            DependencyInjectionTestExecutionListener.class,
            DirtiesContextTestExecutionListener.class,
            TransactionalTestExecutionListener.class
    })
    public class SpringAppTestCase extends Assert implements ApplicationContextAware {

        protected JndiTemplate jndiTemplate = new JndiTemplate();

        @Autowired
        protected JdbcTemplate jdbcTemplate;

        protected ApplicationContext applicationContext;

        public void setApplicationContext(ApplicationContext ac) {
            this.applicationContext = ac;
        }

    // 
    //    @Test
    //    public void doit(){
    //      // this would prevent blow up but 
    // all subclass tests would run an extra method
    //    }

        protected Logger log = Logger.getLogger(getClass().getName());

}

The error:

java.lang.Exception: No runnable methods
    at org.junit.internal.runners.MethodValidator.validateInstanceMethods(MethodValidator.java:32)
    at org.junit.internal.runners.MethodValidator.validateMethodsForDefaultRunner(MethodValidator.java:43)
    at org.junit.internal.runners.JUnit4ClassRunner.validate(JUnit4ClassRunner.java:36)
    at org.junit.internal.runners.JUnit4ClassRunner.<init>(JUnit4ClassRunner.java:27)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:76)

Upvotes: 42

Views: 45929

Answers (9)

iamcoming
iamcoming

Reputation: 141

In my case, spring boot with JUnit 5, I use @ExtendWith(SpringExtension.class) along with @RunWith(MockitoJUnitRunner.class), remove @RunWith(MockitoJUnitRunner.class) fixed it.

Upvotes: 0

Oluwasegun Ilori
Oluwasegun Ilori

Reputation: 11

I made the base unit test class an abstract class. Then Junit no longer sees it as a test class.

Upvotes: 1

TOUDIdel
TOUDIdel

Reputation: 1304

Make sure you do not mix JUnit4 and JUnit5.

For example try to use @BeforeEach (from JUnit5) instead of @Before (from JUnit4) etc.

And make sure your @Test annotation is imported from org.junit.jupiter.api.Test instead of org.junit.Test.

Upvotes: 2

I got this error because my test class is not public.

Upvotes: 0

Nkurunungi Brian
Nkurunungi Brian

Reputation: 1

You want to make sure that you are not importing the @Test from Jupiter, mixing the JUnit 4 and JUnit 5, can cause the same issue. I imported the right JUnit for my SpringRunner and the problem was resolved

Upvotes: 0

Zixel
Zixel

Reputation: 481

I Got the same problem but I had no class inheritance and @Test annotation was present. Problem was that my test class was public, I deleted public modifier and problem was fixed.

Upvotes: 2

akinmail
akinmail

Reputation: 668

making sure the @Test annotation import is org.junit.Test and not something else like juniper which i saw as a comment by here worked for me. Wanted to add this as an answer so it can be seen by others

Upvotes: 17

Soumyajit Swain
Soumyajit Swain

Reputation: 1340

I had the same issue. I was testing without a Test method.

@Test
public void setupTest() {
}

Above method solved the issue.

Upvotes: 10

Robert Munteanu
Robert Munteanu

Reputation: 68268

Make the parent class abstract or rename it such as it does not end in Test or TestCase.

Upvotes: 88

Related Questions