JUnit methods execution sequence
In order to be effective in your unit tests, you need to know in details how JUnit works. In this post, I’ll show what is the execution sequence in one JUnit test. The code shown in the current post is available on GitHub java-samples/junit repository.
JUnit execution sequence
Methods that are used in a JUnit test:- Methods annotated with @Before and @After - those are public void methods that do some specific setup/teardown before and after a test method. Generally, it is good to have just one @Before and @After method, but JUnit allows as much as you have. Execution sequence with the same annotation is in order of appearance in the file.
- Methods annotated with @BeforeClass and @AfterClass - those public static void methods which do some setup/teardown just once before and after all tests have started/passed. An also good idea to have just one of each, but in case of more with the same annotation, they are executed in order of appearance.
- Methods annotated with @Test - those are public void methods with actual tests logic and asserts. Generally, there should be many test methods in a class. Default JUnit execution order is by name ascending. Still, this is not always guaranteed. Although bad practice to have a sequence of unit tests this can be done by annotating your test class with @FixMethodOrder(MethodSorters.NAME_ASCENDING).
- Test class constructor - each and every test method is run in its own object instance, so constructor is run on instantiation. It is not very good practice to do something in test class constructor. Setup should be done in a @Before method.
@BeforeClass
TestClass constructor
@Before
test1 body
@After
TestClass constructor
@Before
test2 body
@After
TestClass constructor
@Before
test3 body
@After
@AfterClass
JUnit execution sequence with rules
In Use JUnit rules to debug failed API tests post, I have described rules and how they work. TestWatcher gives access to tests results when the test starts and finishes. The example here is with @Rule and @ClassRule annotated objects from a custom PrintSequenceRule class extending TestWatcher class. Along with methods described in the previous section, there are several that come into play when rules are involved:- starting() and finished() - methods come from TestWatcher class and are run on start/finish of class/method.
- succeeded(), failed() or skipped() - one of this is executed based on class method result.
- Rule constructor - it is not recommended to use something in rule constructor.
RuleClass constructor
starting() of TestClass
@BeforeClass
RuleClass constructor
TestClass constructor
starting() of TestMethod test1()
@Before
test1 body
@After
succeeded() of TestMethod test1()
finished() of TestMethod test1()
RuleClass constructor
TestClass constructor
starting() of TestMethod test2()
@Before
test2 body
@After
succeeded() of TestMethod test2()
finished() of TestMethod test2()
RuleClass constructor
TestClass constructor
starting() of TestMethod test3()
@Before
test3 body
@After
succeeded() of TestMethod test3()
finished() of TestMethod test3()
@AfterClass
succeeded() of TestClass
finished() of TestClass