4

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;
      }

  }
}
Community
  • 1
  • 1
user2061466
  • 485
  • 9
  • 17
  • 27
  • are you using junit4? – Woot4Moo May 29 '13 at 12:53
  • @Woot4Moo Yes, I am using Junit 4.11 in Eclipse – user2061466 May 29 '13 at 22:12
  • Are you sure it is being run as a JUnit 4 test? Sometimes Eclipse defaults to JUnit 3. The way to check is to do run as ... to see the launch configuration for that test. – Jeanne Boyarsky May 31 '13 at 14:58
  • @JeanneBoyarsky 1. I clicked on the _Run As_ arrow icon on the top, then select the Run As option->Eclipse showed blank for the Run As section. 2. Execute test suite by right click on the driver class, then choose Run As option->choose Junit Test-->The Junit Pane showed the result as **UnitTestDriver[Runner:Junit4]**. image show Eclipse's run configuration setting http://tinyurl.com/mvt2jfa. Please advise me how to what went wrong in this case. Thanks! – user2061466 Jun 01 '13 at 15:36
  • You are running it correctly. I see the problem now (posted as an answer.) – Jeanne Boyarsky Jun 01 '13 at 17:47
  • To improve the readability of your code you should consider to follow the [Java naming conventions](http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367) for classes. – Jens Piegsa Jun 01 '13 at 18:02

1 Answers1

4

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.

Jeanne Boyarsky
  • 12,156
  • 2
  • 49
  • 59
  • 1
    +1 for mentioning the Enclosed test runner, which helps to organize test methods hierarchically. – Jens Piegsa Jun 01 '13 at 17:55
  • @JeanneBoyarsky. Thanks providing me with detailed coding samples. I run into some issue while doing method 2-carry out the test within the nested class. I added *`import org.junit.runners.Enclosed;`*, Eclipse complained _The import org.junit.runners.Enclosed cannot be resolved_. for `@RunWith(Enclosed.class)`, I received error message saying _Class cannot be resolved to a type_ and _Enclosed cannot be resolved to a type_. Please advise me how to make it work. Thanks! – user2061466 Jun 03 '13 at 12:49
  • What version of JUnit are you on? I believe Enclosed used to be experimental and in a different package. – Jeanne Boyarsky Jun 03 '13 at 22:47
  • @Jeanne my Junit version is 4.11. Shall I download the newest Junit jar file to resolve this issue? – user2061466 Jun 05 '13 at 04:14
  • @JeanneBoyarsky I added junit.4.5.jar file to Eclipse's Java build path. To test method 2 using enclosed class. Eclipse showed the error message _java.lang.TypeNotPresentException: Type Enclosed not present_. Could you advise me what went wrong in my code? Thanks! – user2061466 Jun 08 '13 at 21:19
  • I don't know what to tell you. It works fine for me on JUnit 4.8. Are you trying my example exactly as is? – Jeanne Boyarsky Jun 08 '13 at 22:36