user2061466
user2061466

Reputation: 485

Junit - java.lang.Exception: No runnable methods

I am doing a tutorial on execute server test cases by using a driver class. The Eclipse error pane showed error message: java.lang.Exception: No runnable methods. The error message occurred when driver class tried to run exceptionTesting class.

Based pm this post: java.lang.exception no runnable methods junit. It seems like @Test annotation should be used in the test class or updating Junit Jar file to most current version will solve the issue.

Junit Jar file version: 4.11

Please review my code and advise me what modification shall I do to avoid the error message. Thanks!

Driver Class:

import org.junit.runner.*;
import org.junit.runners.Suite;

 @RunWith(Suite.class)
 @Suite.SuiteClasses({
     basicAnnotation.class,
     personTest.class,
     exceptionTesting.class
 })
public class UnitTestDriver {

} 

exceptionTesting class

import org.junit.*;
import org.junit.Test;


public class exceptionTesting 
{
  public class junitTest2{
      @Test (expected = ArithmeticException.class)
      public void divisionWithException(){
          int i = 1/0;
      }

  }
}

Upvotes: 4

Views: 21519

Answers (1)

Jeanne Boyarsky
Jeanne Boyarsky

Reputation: 12266

The problem is that junitTest2 is nested inside of exceptionTesting. You have two choices to solve it:

1) Get rid of the inner class having your tests in the top level class:

import org.junit.*;
import org.junit.Test;


public class exceptionTesting 
{
      @Test (expected = ArithmeticException.class)
      public void divisionWithException(){
          int i = 1/0;
      }
}

2) Have your top level class specify the tests are enclosed and make your inner class static:

import org.junit.*;
import org.junit.Test;

@RunWith(Enclosed.class)
public class exceptionTesting 
{
  public static class junitTest2{
      @Test (expected = ArithmeticException.class)
      public void divisionWithException(){
          int i = 1/0;
      }

  }
}

Unless you have some reason to need the inner class, #1 is the better choice. Also note that in Java, convention is to begin classes with uppercase names. So exceptionTesting would become ExceptionTesting.

Upvotes: 4

Related Questions