Must Know Junit Rules
Must know Junit Rules
In this blog post I will be writing about some Junit Rules that are very useful while writing test cases. This is not a introduction to Junit Rules. I will just be writing about some of the rules that Junit provides out of the box and how they could be extended to your use case.
1.Expected Exception: As the name suggests, this is for verifying the exception thrown when you invoke a method. This provides good helper methods by which you can verify the cause of the exception, the message in the exception and much more. This greatky reduces the amount of boiler plate that we write for verifying the exceptions. Below is a simple code snippet showing the usage of this rule,
2.External Resource: This rule can be used when you are accessing some external resource like a database in your tests. This is an abstract rule, which provide two methods before and after which would contain but the setup and teardown logic respectively. This the rule I found most useful, this rule can be for various resources like contacting external database, setting up and teardown in-memory database etc. You can define a rule for the database you are using in your project and reuse it in all the test classes, pretty cool!. Below is a simple code snippet showing the usage.
3.Temporary Folder: You can use this rule if you need a temporary folder in tests to create and delete files. Below is a code snippet showing the usage.
4.Timout: As the name says, you can add timeouts to your tests. The tests will fail if they do not complete in the set timeout. This rule is useful when you are invoking some remote services and having a timeout on such tests make sense.
5.Verifier: This rule could be used to add global verifications in your test classes.