How to run a single JUnit test method in Eclipse?

For running a single test case in Eclipse (as per your last comment):

  • Go to Run (the green forward arrow button) -> Run Configurations.
  • Right click on JUnit, and select new.
  • Fill in your test case and test method (the Search button is really helpful here).
  • Click Run.

It seems that now days (Eclispse 4.4.0) this can be done easily.

If you place the caret at the head of the definition of a test case method, issue the Run or Debug command (menu or F11 or Ctrl+F11), then the JUnit plugin runs only that test case.

(If you on the other hand place the caret in the body of a method then all the test cases in that class are run.)

Some more tips for running JUnit tests:

  • The Go to Previous/Next Member commands can be used to quickly move the caret to the head of the definition of a method with the keyboard. The default key bindings are Ctrl+Shift+Up/Down.
  • If the Run or Debug commands are issued when the Debug, JUnit or Console views are active, then Eclipse will run the last run configuration. This can be used to re-run your single test case without having to navigate back to the editor.
  • Running a particular run configuration can be done by navigating the Run menu: Alt + R, H, number key.

I think what you want to do is label your tests as belonging to different JUnit Categories and then run just those from one or more categories and not all tests, using the @RunWith and @Categories annotations. It's how I've done it in the past. In your case, you may have a category with just one test.

See examples:

Running benchmark methods in a JUnit test class

How to run all tests belonging to a certain Category in JUnit 4