Using @Rule and TemporaryFolder - JUnit Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to take a look at how we can use the rule annotation which comes out of the box with junit we're going to take a look at how the rule annotation actually works and some of the key use cases that come with the rule annotation with junit and then to conclude this video we'll then run through an example of how the raw annotation can be used creating a temporary folder that can be later used within our individual unit tests i've created this example test to illustrate how the raw annotation may be working from a conceptual standpoint so you may be familiar with the before annotation and also the after annotation and the way the before annotation works is that this method call will be made before each individual test case of the entire test class the after annotation will be called at the end of each unit test within our entire test class so if i were to run this individual test one here we would expect it to print out set up test one and then tear down so we can see these three lines are printed below so the rule annotation works in a similar way in the sense that it wraps each unit test of your test class with a before and after annotation where it performs some actions before the test runs and then some actions after the test runs now if we take the rule annotation out of the box that comes with junit there are quite a few specific use cases for when we would want to use the rule annotation within our unit tests the rule annotation can be used for any class that implements the test rule interface so if we have a look at the test rule interface we can see that this is the code that comes with junit out of the box and we can also see quite a few examples here of how the raw annotation is already implemented with junit so you can use this error collector which will collect multiple errors in one test method then we also have a few more here for the expected exception creating temporary folders extracting the test name we also have this test watcher we also have a timeout so you can time how long your test can run for before failing automatically and also verifier so you can create your own role annotation implementation just by implementing this test draw interface i'll leave the link to the documentation for the test rule and the rule annotation in the description of this video but to move on we're going to look at how we can use this temporary folder implementation of the raw annotation to demonstrate how the rule annotation might be used within practice so if we take a look at the class that we'll be testing it's called csv line counter and it has a single method called count lines from file that takes in a file path it uses the files.lines method and the dot count method to return the number of lines that we have within that file we're now going to begin writing the unit test for this single method so i'm going to move into the csv line counter test class and we can begin writing this first unit test for counting the lines so if we look over this test we can see that we're first creating a file which will be using the relative location of this test class and i'll be creating a file called test csv we then define some csv data which is just the apc followed by newline and then def and then finally g and then we'll be writing this csv data into this test file and then we'll be closing that buffered writer we'll then be using this exact same file by calling the actual lines method from our csv line counter and we'll pass in the path of our file and then we have this assertion down here where we expect the number of lines to be three and that will be compared to the actual lines which are returned from the csv line counter so i'm going to run this test and see what happens [Music] okay so the test has passed we cannot we can also see on the left hand side that our test csv file has actually been created if we open it we can see that expected three lines where abc def and then g is printed within it the problem we have with our unit test is that it's going to be adding this csv file every time that we run this test and this will add to the overall size and memory consumption of our application if we have to actually create real files to actually test our application code so what we can do is delete the test.csv file after our test has been run or after the assertion of the test has been made and we can do that by using a finally block [Music] so if i delete this file and rerun the test hopefully that new test.csv file is not created even after the test is run so the test has passed and we can see that the csv file has not been retained within the source folder i'm just going to see what happens if we have a failing test to see if that file is left there or not so even if the test fails our file is still not created so that's quite a neat way we have of testing our count lines from file methods but there is a way that we can improve the efficiency of our unit test by using the temporary folder raw annotation so to begin using the raw annotation i'm going to put at and then rule at the top and this will define one of the many different implementations that we can use of that raw annotation that we saw earlier so i'm going to define a public temporary folder [Music] and i'm going to instantiate at the top [Music] so this instance of a temporary folder will create a temporary folder as designated by junit and what makes it special is that it will automatically be created for each test case that we have within our test class and then regardless of if the test fails or passes it will then delete that exact same folder for us without us having to do anything so that will handle the creation and deletion of these test files that we might want to use within our test case so now if we're going to use the temporary folder we no longer have to delete the file from the end and i can also remove this try block the file that we create will no longer be pointing to a relative directory but instead we'll be using this temporary folder to define where our file is created and then the remainder of the method remains the same where we have the csv data we have the buffered writer which will then write this csv data into our temporary file the buffer writer will then close we can perform our assertion upon the number of lines within that file and then we know that thanks to this raw annotation and the temporary folder instance that we're using the creation and deletion of this file will now be handled by the rule annotation and we no longer have to do that ourselves within the unit test itself so i can run the test [Music] and we can see that the test file that we used within the test has not been created anywhere locally and we know that thanks to the temporary folder instance that we're using it will be automatically deleted whether this test passes or fails so that summarizes this video on a single case study of how we can use the raw annotation and specifically the temporary folder it would be good to have a look at all the other instances of the raw annotation that you can use out of the box with junit and how they can assist you in writing more efficient and cleaner looking unit tests with junit
Info
Channel: Aneesh Mistry
Views: 2,396
Rating: undefined out of 5
Keywords: Software Engineering, Software Development, JUnit, RuleAnnotation, Tutorial, Java, Aneesh Mistry Tutorial, Aneesh Tutorial, Aneesh Mistry, Java 8, Software Tutorial, Software Engineer, Mockito Tutorial, Junit Tutorial, Backend Engineer, IntelliJ, How to test software, Test Automation, testing software, beginner software engineer, learn to code, learning to code, step by step tutorial, JUnit Tutorial, @Rule, IntelliJ tutorial, JUnit Mockito
Id: KzJ89AFewRM
Channel Id: undefined
Length: 8min 52sec (532 seconds)
Published: Mon Jun 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.