via Mockito mock static method example | Examples Java Code Geeks.
In this article, I am going to show you how to mock static methods using Powermockito.
PowerMock is an open source mocking library. It extends the existing mocking frameworks, such as EasyMocks and Mockito, to add even more powerful features. One of them being mocking static methods. For more information, read Powermock – Mockito Integration Example.
My setup details:
via Powermock – Mockito Integration Example | Examples Java Code Geeks.
Most of the mocking frameworks in Java, including Mockito, cannot mock static methods or final classes. If we come across a situation where we need to test these components, we won’t be able to unless we re-factor the code and make them testable. For example:
- Making private methods packaged or protected
- Avoiding static methods
But re-factoring at the cost of good design may not always be the right solution.
In such scenarios, it makes sense to use a testing framework like Powermock which allows us to mock even the static, final and private methods.
via Mockito void Method Example | Examples Java Code Geeks.
In Mockito Hello World Example, we have learnt how to stub a non-void method that returns something. Sometimes we may also need to stub a void method which is what I am going to show in this article.
Before I start with my example, a bit about my setup:
via Mockito Spy Example | Examples Java Code Geeks.
In this article, I am going to show you an example of Mockito Spy.
There are times when we would like to use most of the original object’s behavior but mock only a portion of it. This is called spying objects, also called as partial mocking. Before I start with the example, let me first brief you about my setup:
Click to Read: I sometimes use FizzBuzz to demonstrate the basics of unit testing to newbies. Although FizzBuzz is really simple problem, it can also be used to demonstrate more advanced unit testing techniques like mocking.