How to verify static methods with PowerMock - Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video is going to look at how you can verify that a static method has been called using the Mojito and power mock libraries if you're new to this channel I create Wiki tutorials just like this one on Technologies such as Java and angular so please subscribe if you're interested to see more so we're going to take a look at a pretty unique test that involves verifying that a static method has been called in a previous video which I will add the cardio in the top right I've looked at how we can use mojito verify to assert that certain methods have been called how many times that they've been invoked and also the arguments that they have received so in this video we're slightly extending upon that to include the static methods so let's take a look at a class that we'll be testing it's called male person and it has just one method called deliver mail and it will accept an address as a string if the address contains ABC the male person will use the sent method from the mail engine class and the mail engine is a class that is being passed in through the Constructor however if the address contains ABD it will use the send mail to ABD method of the mail engine class instead so the mail engine class contains two methods we have send which will print out send non-static and that's just for us to see and we also have a static method which will print out static sending to ABD so what we want to achieve in this video is to verify that when we pass in an address that contains ABD that the send mail to ABD method has been called but before I write the test I'm going to quickly run over our dependencies and to also write a test for the ABC method just to refresh our minds about makito verify so in our Palm XML file I have two dependencies the power mock module junit 4 and the power mock API mojito 2 dependencies in version 2.0.2 we'll understand in just a moment why we need these two I have also left a link to the source code in the description for you to check out so let's head into our mail person test class and I'm going to first write a test using my keto verify for the send method so I'm going to define a mock instance of the male engine and then pass that into an instance of the male person next I'm going to call the deliver mail method passing in an address of ABC so if we head into the mail person method we will expect the first if condition to return true and then back in the mail person test class I can call verify passing in the mail engine Mock and then the dot send method so I'm just going to quickly run this and we can see it passing if I change the address to a b d c and rerun the test we can see that it's fading because that send method is now Accord ing okay so your first thought might be that we can simply change the address to ABD and then verify that the send mail to ABD method has been called and this will verify that the static method has been called so if I make the changes we can see the code compile uh so far so good if I run the test we can see it pass and in the console output however we can see that static sending to ABD has been printed twice so that's because our verify method isn't actually verifying the static method being called it's actually just calling it if I change the address to a b a d we would expect this test to fail as send mail to ABD will not be called and instead what we can see is that it's still passing so what we have printed is invalid address as neither of the if statements are true and then the static sending to ABD is called because the verify assertion is not verifying any calls but it's instead it's just calling that method itself so this isn't actually testing the static method as we would like so I'm going to add a new test for verifying our static method as being called before we do this I will need to use the power mock library and to instruct our test class with two annotations at the top so first I will tell our test class that it will be used to mock static methods it would also be used amongst many other supported types of power mocks such as final classes and we're going to do that with the run with annotation passing in the power mock Runner class this annotation comes from our power mock module junit 4 package as we earlier saw in the pom XML file the remaining methods and annotations come from the power mock API mokito 2 package so next I will add the prepare for test annotation and this will be taking in the mail engine class and this annotation will instruct power mock to prepare this class for bytecode manipulation and that will enable us to assert against the static methods which is something we haven't been able to do as we saw earlier in this video and then within our test I will enable static mocking on our class by calling power moquito mocstatic method passing in that mail engine class again I will then create a new mail person instance passing in a new mail engine instance for an argument and then I'm going to call the deliver mail method with ABD so our expectation now is that the send mail to ABD method will be called so we can assert Upon This by first telling power more keto to verify a static method so we can assert Upon This by first telling power monkey to verify a static method and this will accept two arguments first it will be the class that is being mocked and then the number of times the method will be called then on the line below I will reference the static method that is being verified so I'm just going to run over that one more time to make it clear so we add run with annotation to tell this class it will be mocking with power mock we prepare for test the mail engine to tell the course it will be mocking this class with power mock then in the test method we perform the mocking of the class we call the deliver method with abz and then to verify that our method has been called we have two steps so first to mention the class and the number of times it's going to be invoked and then the second step is to specify the static method that is actually being invoked now if I run the test we can see that it's passing and if I change the address to a b a d we can see the method fail and if I change the number of invocations to two we can see it fail one more time so that concludes our video on how you can use powermock to verify that a static method has been called once again please check out the link in the description to the source code so you can try it out for yourself
Info
Channel: Aneesh Mistry
Views: 2,322
Rating: undefined out of 5
Keywords: Software Engineering, Software Development, JUnit, Java, Aneesh Mistry, Aneesh Mistry Tutorials, Aneesh Tutorials, Software Engineer, software engineer, Mockito, Coding, IntelliJ, spring boot, Spring, Spring Boot, Learn to code, software testing, automation testing, Maven, Tutorial, Java 8, Instructional course, Software developer, JUNit tutorial, Mockito tutorial, Powermock, Powermock mock static, mockStatic, verifyStatic, Powermock testing, Mockito testing, IntelliJ tutorial
Id: w12cuVpqf7w
Channel Id: undefined
Length: 8min 6sec (486 seconds)
Published: Fri Oct 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.