1

In JUnit, setUp() is called before running "each" test case and tearDown() on completion of the run. But I want to know whether there is method that get called after running "all" test cases in the suite.

I need such a method to insert some instrumentation.

himaFe
  • 21
  • 1
  • 5
  • this https://stackoverflow.com/questions/82949/before-and-after-suite-execution-hook-in-junit-4-x might help – Eugene Jul 19 '17 at 20:35
  • There is a substantial difference between JUnit 3 and 4. Which one are you talking about? It kind of sounds like 3, which is now heavily outdated. – SiKing Jul 19 '17 at 20:52
  • @SiKing It is Junit4 – himaFe Jul 19 '17 at 21:01
  • If you are using JUnit4, then the name of the method is irrelevant. JUnit4 uses annotations, as described by the answers here. – SiKing Jul 19 '17 at 22:33

2 Answers2

2

@After annotation is used to indicate the method to be run after every @Test

Also, for reference:

To run method before a test: @Before - executes before each @Test, @BeforeClass - executes before all @Tests

To run method after a test: @After - executes after each @Test, @AfterClass - executes after all @Tests

slal
  • 2,657
  • 18
  • 29
  • Are you suggesting the solution mentioned here ?? stackoverflow.com/questions/9903341/… If that is the case, should I add all my test cases in @SuiteClasses?? – himaFe Jul 20 '17 at 19:14
  • here is a simple example: http://junit.sourceforge.net/javadoc/org/junit/After.html – slal Jul 20 '17 at 20:00
  • there is nothing about 'after ALL test cases' in this answer. – Mikhail Bolotov Aug 24 '21 at 18:51
0

Look at the @BeforeClass and @AfterClass annotations. They identify static methods to run before/after all the tests

David Zimmerman
  • 335
  • 4
  • 7
  • 1
    that's running after a class, not after *all* of them – Eugene Jul 19 '17 at 20:34
  • Right; not running after *each* test, but running once, only after all of the tests have run – David Zimmerman Jul 19 '17 at 20:39
  • Are you suggesting the solution mentioned here ?? https://stackoverflow.com/questions/9903341/cleanup-after-all-junit-tests If that is the case, should I add all my test cases in @SuiteClasses?? – himaFe Jul 19 '17 at 20:50
  • No need, the \@AfterClass will run *once* *after* all the tests. Any clean-up you need to do can be done there. Even if you use your IDE to run just one test, the \@BeforeClass and \@AfterClass will run *once* – David Zimmerman Jul 20 '17 at 13:33