junit

JUnit No Runnable Methods

In this tutorial, we shall show users the usage of JUnit and explains the reason behind the No Runnable Methods error. We live in a fast age of technology where the everyday new breakthrough is going on. We have to be updated with the latest technologies around us and how they affect our lives.

We have seen in our previous tutorial about the running parallel tests in JUnit and how we can run our tests in parallel so that we can use the multi-core functionality of the latest CPU’s.

Similarly, with the enhancements in the JUnit framework, we have seen a lot of different situations where we have to do nothing and everything is handled by JUnit itself. In this tutorial, we will see how we can get rid of the no runnable method error.

1. Introduction

JUnit no runnable methods error arises when we do not specify any method for testing. Previous versions of the JUnit uses the testXXX() style of naming methods. So whenever JUnit runs it will see the methods starting with test. It will run all methods present in the class.

But as the JUnit advances to the next level it defines its new way of finding the test methods in a class. Today JUnit uses @Test annotation. We simply apply that annotation on a method and JUnit runs it. There will be no boundation of naming the methods starting with test.

@Test annotation plays a vital role running our test cases. We have used @Test annotation in our example to show this.

2. Technologies Used

We will be using the following technologies in our example.

  • Java 8 – We will be using the latest version of Java. There is no specific feature of Java 1.8 that has been used.
  • JUnit 4.12 – This is the main testing framework that we will be using.
  • Maven – This is the build and dependency tool for this example.
  • Eclipse – IDE for writing the code.

For links to the latest versions and their websites visit the references section of the tutorial. Now, we have defined the technologies to be used. Let’s start the setup of our example.

3. Project Setup

Tip
You may skip project creation and jump directly to the beginning of the example below.

Start by opening eclipse. Click on File -> New -> Maven Project. You will be shown the following screen.

Simply check the first check mark and click on the Next button.

JUnit No Runnable Methods Setup 1
Figure 1: JUnit No Runnable Methods Setup 1

On this screen, you need to fill in the details regarding the project we are building. You can simply fill in the following details as shown and click on the Finish button.

JUnit No Runnable Methods Setup 2
Figure 2: JUnit No Runnable Methods Setup 2

After clicking on the Finish button, we are ready with the blank Maven project. But before we start with the code, we need to do some changes in the pom.xml file.

pom.xml

 
...
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
 </dependencies>
 <build>
     <finalName>junitnorunnable</finalName>
     <plugins>
         <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-compiler-plugin</artifactId>
             <version>2.5.1</version>
             <inherited>true</inherited>
             <configuration>
                 <source>1.8</source>
                 <target>1.8</target>
             </configuration>
          </plugin>
     </plugins>
 </build>
...

Line 4: actually pulls out the JUnit for this project.
Line 18,19: tells maven to compile and build with Java 8.

4. JUnit No Runnable Methods

First, we need to create a class where no @Test annotation is used. It is a simple class with a single method for testing. Since we are using the eclipse it will not run the class as a JUnit class because of the method with any @Test annotation.
So, we will create another class with Suite runner. You can get the example of suite runner in references sections.

TestClass.java

 
package junitnorunnable;

public class TestClass {
    
    public void testA(){
        System.out.println("Hello");
    }
}

Next, we will create a Suite class.

TestSuite.java

package junitnorunnable;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({TestClass.class})
public class TestSuite {

} 

So, when you try to run this class you will get the error as shown below. Yes, definitely it is the same error that we are dealing with.

JUnit No Runnable Methods Error Output
Figure 3: JUnit No Runnable Methods Error Output

Now, if we add the @Test annotation to our class, then test case will be passed.

TestClass.java

 
package junitnorunnable;

import org.junit.Test;

public class TestClass {
    
    @Test
    public void testA(){
        System.out.println("Hello");
    }
}

Here we have added @Test annotation on our method (line 7). When we run the TestSuite class we will get the following output.

JUnit No Runnable Methods Success Output
Figure 4: JUnit No Runnable Methods Success Output

You can see, that by applying the @Test annotation our error is resolved.

5. Conclusion

JUnit no runnable methods is an error that is generated whenever JUnit finds no test methods to run. There is a simple way to deal with it. Either name the method starts with test or use the @Test annotation on the method.

This clears why we get this error. And we had also seen the solution to this problem. References to some of the common libraries used can be seen below.

6. Download the Eclipse Project

This is an example of the JUnit no runnable methods.

Download
You can download the full source code of this example here: JUnitNoRunnable.zip

7. References

  1. JUnit 4.12
  2. Maven
  3. Suite Runner Example

Vinod Kumar Kashyap

Vinod is Sun Certified and love to work in Java and related technologies. Having more than 13 years of experience, he had developed software's including technologies like Java, Hibernate, Struts, Spring, HTML 5, jQuery, CSS, Web Services, MongoDB, AngularJS, AWS. He is also a JUG Leader of Chandigarh Java User Group.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button