Updated April 3, 2023
What is JUnit Rule?
Junit Rule provides control to the users to execute a certain functionality before and after a test is run. The functionality includes, creation of files and subdirectories before execution of a test case and deleting them post completion of it, run a time check on the test case execution and aborting the test in case of time overrun, enabling database connectivity prior to execution, and disconnecting it post-execution of test cases.
There could be some routines, common to all the test cases, to be executed before the start and at the end of test cases. Such routines can become part of Junit Rule which can be called in all test cases. It is mandatory that these Junit Rule components must use org.junit.rules.TestRule interface. It is important that Junit rule fields should be pubic, not static and it should be one of the Testrule subtypes.
Guide to Junit Rules
Brief explanations of the various rules available in Junit that can be used to manage Test cases are listed below.
|External Resource Rule
|External Resources may be set up before the start of the test case and it can be dropped later. Connectivity to a file or database can be enabled through this abstract class.
|Error Collector Rule
|This rule collects the error details as it occurs in the test case execution. The execution will not stop due to the errors but the test will fail at the end. The details collected will give good data for analysis of errors and taking corrective actions
|Test Name Rule
|This rule facilitates the display of the current test case name when the test is executed
|This rule helps developers to verify a few behaviors in the test cases. Extra verification logic should be added appropriately to verify the intended activity.
|Earlier developers used to set time-out values through an annotation @Test in each and every test case individually. But this rule helps to set this timeout at a global level which will become applicable for all the test cases uniformly.
Test cases fail automatically when the execution of test cases takes more time than the time-out limit.
|Expected Exception Rule
|There could be many known or unknown errors during the execution of test cases. The known expected errors can be trapped inside the test case module in the error-handling routine. When the expected error occurs the exception handling routine gets control and the execution can be brought to a logical halt.
|Test Watchman Rule
|This rule monitors the performance of the test cases and logs are maintained for each and every successful / fallers test case.
|Disable on debug Rule
|Developers would like to switch off some of the rules when they debug the code. The timeout rule should be disabled when the program is executed in debug mode to complete debugging. The unique advantage of this rule is that it will not alter any condition when the test case runs in normal mode.
|Temporary Folder Rule
|It Will be discussed in detail as a separate topic
|Applying Class Rule
|All the above rules will apply at the test cases level only and there may be a requirement that some rules may have to be applied around all tests. This rule will facilitate applying such conditions but the field referenced in this rule should be static.
|So far, we have seen standard rules which are inbuilt into JUnit available as of box solution to the users. These rules cover the entire users’ community and their needs. But there could be one-off specific requirements of some users, specific to an application, which need to be addressed through custom development of rules. Developers can write their own rules and implement them in their applications.
Temporary Folder Rule
Out of so many rules explained above, let us take this rule in detail and deep dive into it.
What is it?
The temporary folder rule is the logical extension of the Extension Resource rule and this rule enables developers to create files, folders, and sub-folders that are required for running the test cases. The working data that are created during the execution of test cases, can be stored in these files and folders and these transit or temporary files/folders can be deleted once the execution is over.
Generally, the files or folders will get deleted and this rule does not check whether the deletion had been successful or not and it does not throw any exception as such.
Method Name Access Specifier, Return Type Details.
|Access Specifier, Return Type
|Override function to delete the external resources
|Override function to set up the external resource
|This is used only for testing purposes
|This will delete all the files / folders in the temporary folders
|Returns the path object of the root component or null if there is no root
|Returns a file (new and fresh) with random name in the temporary folder
|newfile(string, file name)
|A new fresh file with the given file name in the temporary folder is returned
|New fresh folder with a random name in the temporary folder is returned
|New fresh folders with given folder names in the temporary folder is returned
|New fresh folder with the given folder name in the temporary folder is returned.
|Inherited from org.junit.rules.externalresources class
|Inherited from java.lang.object class
|– do –
|– do –
|– do –
|– do –
|– do –
|– do –
|– do –
Conclusion – JUnit Rule
Junit offers powerful and flexible facilities to developers to manage test cases effectively with the least effort and costs. Another important advantage of this tool is that it allows developers to have their own way of creating any rules (if it is available as out-of-box solutions) to suit their one-time needs to manage test cases.
This is a guide to Junit Rule. Here we discuss the What is Junit Rule, a guide to Junit rules, Temporary Folder Rule, methods. You may also have a look at the following articles to learn more –