java lang exception no runnable methods
In Java, the java lang exception no runnable methods error is basically an exception of Junit, which occurs when Junit is unable to find the runnable test methods. In the spring boot, i.e., one of the most used Java frameworks throws the no runnable methods exception where no runnable test methods are configured in the test classes of Junit.
The Junit framework plays a vital role in the testing of the Java classes. In the past decade, the Junit test methods are determined by the method signature. In the early version of the Junit framework, it searches for the methods which start with the keyword "test". In order to perform the unit test of the Java source code, it runs all the methods in the test class that start with "test".
In the current version of the Junit framework, it searches all the methods that are configured with @Test annotation, not the methods which are starting with the keyword "test".
Let's take an example and try to produce the no runnable methods exception. In our example, we will use Java 8, Junit 4.12 for testing our java source code, Maven, i.e., is build and dependency tool, and Eclipse, i.e., IDE for writing the code.
We will create a new maven project using Eclipse IDE tool and add the following dependency in the pom.xml file.
Let's create a simple class NoRunnableMethodsExample to test.
Now, we write the test class for the above class without defining the test method in the following way:
Now, if we try to run the test class, it will definitely throw the no runnable methods exception.
Resolve exception error
In order to remove the exception, we have to define the runnable methods that call the method of our mains class to check whether they return the expected result or not. We modify the NoRunnableMethodExceptionTest.java class in the following way:
So, the java lang exception no runnable methods exception throws by the Junit when it founds no test methods to run. We can easily solve the exception by either defining method that starts with the test or use the @Test annotation on the method.