15

In a JUnit test case with multiple @Test annotations, how does one selectively run tests ?

For e.g., from the following code, how does one run just one test method ?

   @Test
   public void testHelloEmpty() 
   {
      assertEquals(h.getName(),"");
      assertEquals(h.getMessage(),"Hello!");
   }

   @Test
   public void testHelloWorld() 
   {
      // h.setName("World");
      assertEquals(h.getName(),"World");
      assertEquals(h.getMessage(),"Hello World!");
   }

I have tried to just highlight one @Test method and tried to run it, but it doesn't work that way.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
AussieMoss
  • 185
  • 1
  • 1
  • 7

4 Answers4

10

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.
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 4
    Still seems to be the same in 2015. Holy cow... And I'm expected to do that for each single method I want to run? That probably takes longer than running all test together. Has any Eclipse developer ever seen how this works in Visual Studio? It's two clicks for any test at any time - no need for a configuration. I leave this comment in hope that someone implements this in Eclipse the same way. – Thomas Weller May 27 '15 at 13:23
  • 1
    @Thomas it's possible to do this in newer eclipse versions, see other answers below. – Jeen Broekstra Jan 19 '16 at 03:23
  • InitializationError [Runner:JUnit 4] (0,00s) – StackUser Sep 05 '16 at 18:49
9

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.
Lii
  • 11,553
  • 8
  • 64
  • 88
2

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

Community
  • 1
  • 1
amphibient
  • 29,770
  • 54
  • 146
  • 240
1

Put the cursor on the method name, then press Alt+Shift+X, then T. This will run the test method as JUnit test.

Note: do not put the cursor insode the method. This will run all methods in the class. It must be on the method declaration.

Also note: @BeforeClass test setups might not be executed in case you organize your tests in test suites.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222