EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

JUnit Rule

Secondary Sidebar
JUnit Tutorial
  • Junit
    • JUnit 5 Assert
    • JUnit RunWith
    • JUnit AssertThat
    • JUnit Report
    • JUnit BeforeClass
    • JUnit Framework
    • JUnit Data Provider
    • JUnit Runner
    • JUnit 5 Parameterized Tests
    • JUnit BeforeAll
    • JUnit Private Methods
    • JUnit Integration Test
    • JUnit Maven Dependency
    • JUnit Annotations?
    • JUnit Testing
    • JUnit Test Order
    • JUnit 5 RunWith
    • JUnit 4 Maven
    • JUnit Jar
    • JUnit assertEquals
    • JUnit 5 Parameterized Test
    • JUnit Dependency
    • JUnit Fail
    • JUnit Disable Test
    • JUnit Assert
    • JUnit in Maven
    • JUnit 5 Gradle
    • JUnit XML
    • JUnit XML Format
    • JUnit Eclipse
    • JUnit Test Suite
    • JUnit Parameterized Test
    • JUnit assert exception
    • JUnit Code Coverage
    • JUnit Jupiter
    • JUnit Rule
    • JUnit version
    • Junit Interview Questions
Home Software Development Software Development Tutorials JUnit Tutorial JUnit Rule

JUnit Rule

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Guide to Junit Rules

Brief explanations of the various rules available in Junit that can be used to manage Test cases are listed below.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,064 ratings)
Sl Rule Explanations
1 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.
2 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
3 Test Name Rule This rule facilitates the display of the current test case name when the test is executed
4 Verifier Rule 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.
5 Timeout Rule 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.

6 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.
7 Test Watchman Rule This rule monitors the performance of the test cases and logs are maintained for each and every successful / fallers test case.
8 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.
9 Temporary Folder Rule It Will be discussed in detail as a separate topic
10 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.
11 Customs Rule 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.

Methods used

Method Name Access Specifier, Return Type Details.

Method Name Access Specifier, Return Type Details
after() Protected void Override function to delete the external resources
before() Protected void Override function to set up the external resource
create() void This is used only for testing purposes
delete() void This will delete all the files / folders in the temporary folders
getroot() file Returns the path object of the root component or null if there is no root
newfile() File Returns a file (new and fresh) with random name in the temporary folder
newfile(string, file name) file A new fresh file with the given file name in the temporary folder is returned
newfolder() file New fresh folder with a random name in the temporary folder is returned
newfolder

(string… ,foldernames)

file New fresh folders with given folder names in the temporary folder is returned
newfolder

(string, foldername)

file New fresh folder with the given folder name in the temporary folder is returned.
apply Inherited from org.junit.rules.externalresources class
clone Inherited from java.lang.object class
finalise –          do –
equals –          do –
wait –          do –
toString –          do –
notifyAll –          do –
hashCode –          do –
getClass –          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.

Recommended Articles

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 –

  1. JUnit version
  2. JUnit Annotations 
  3. TestNG vs JUnit
  4. JUnit Maven Dependency
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) Learn More