Unit Testing with Mockito/PowerMockito – Calsoft Blog

on

|

views

and

comments

Is it daunting for you to perform unit testing in Java? Well, there is nothing to worry about as there are many developers who face this challenge. But the good news is that a tool called Mockito can make this process a cakewalk.
Like a veteran, Mockito can potentially fit into different shoes and play any role in the code, making it easier to isolate and test individual components. Primarily, Mockito is a mocking framework. It allows software developers to write impactful tests using clean and simple Application Programming Interfaces (APIs) to execute unit testing for Java-driven applications. Additionally, the tool can be leveraged with other frameworks such as JUnit and TestNG. It also supports the operation of mocks on any system, irrespective of Java installation.
Unit testing using the potential of a mocking framework is recognized as one of the most useful practices for a long duration, and the Mockito framework is known to have dominated this realm in recent years. However, to encourage better code designs and simplify public API creation, some features have been intentionally removed from the tool. Given, the strong reason to do so, these shortcomings push testers to write heavy code, to make the creation of mocks more viable.
Well, this is exactly where Power Mockito comes into play!
But before you thoroughly understand the Mockito and Power Mockito landscapes through our blog, let’s get a preliminary understanding of the basics.
What is Unit Testing?
Unit testing in simple terms is a process that breaks components of a software system into small/individual units that are logically isolated. The primary aim of this process is to scrutinize and validate that each unit of the software meets the required standards. Typically, testers execute unit testing at the early stages of development, even before the code is integrated and tested as a whole. Unit testing is a vital step in software development. When carried out appropriately, it can lead to the early detection of flaws in code, reducing development costs, improving software quality, preventing bugs, and enhancing overall project timelines and efficiency.
What is Mocking?
One of the primary components of unit testing is to get rid of dependencies on the system and replace them with an implementation control. And the most popular way of doing this is by replacing the dependency with a mock. Mocking frameworks are meant to simplify the process.
But what is mocking?
Mocking is a process leveraged to conduct unit testing when the software code unit undergoing the test has external dependencies. The overall agenda of mocking is to separate and isolate the code that is being tested. In mocking, the external dependencies are replaced by closely controlled replacement objects that can stimulate the behavior of the original ones. There exist three main types of replacement objects: Stubs, fakes, and mocks.
What is the difference and connection between Mockito, PowerMock, and PowerMockito?
Mockito is a standard mocking framework that offers a range of functionalities such as mocking, stubbing verifying, etc. However, initially, it was not possible to mock private or static methods.
PowerMock on the other hand deploys a custom class coder and bytecode manipulation to support mocking of constructors, statics methods, removal of static initializers, and more. However, the drawback is PowerMock is that it delivers poor application design.
PowerMockito is an extension API that works in line with Mockito and offers capabilities essential for collaborating with Java Reflection API effortlessly.
Decoding Unit Testing with Mockito/PowerMockito
Mockito is an open-source Mocking framework in Java. The features it provides for unit testing are important. It has streamlined test case writing for developers. While Mockito can help with test case writing, there are certain things it is not engineered to do, namely, mocking or testing private, final, or static methods. That is where PowerMockito comes to the rescue. PowerMockito is designed for testing private, final, or static methods as it makes use of Java Reflection API. However, the most recommended way to begin with Mockito, or PowerMockito is to look for its Maven dependency and associate it with the project. Maven dependency is simple an archive file such as JAR, ZIP, used by Java applications, which is required by the project to create, compile, test, and run.
Here are the dependencies we need to add:
Maven Dependencies
org.mockitomockito-all1.9.5testorg.powermockpowermock-api-mockito1.6.2testorg.powermockpowermock-module-junit41.6.2test
Annotations
Allows shorthand creation of objects required for testing.

Minimizes repetitive mock creation code.
Makes the test class more readable.

Few examples –
@RunWith – Indicates which framework APIs to refer toFor PowerMockito – @RunWith(PowerMockRunner.class) 
For Mockito –  @RunWith(MockitoJUnitRunner.class ) 
@PrepareForTest  -Used only in the case of PowerMockito. This informs PowerMockito which classes to prepare with Java Reflection API for testing. 
@Test – Defines the method as Junit test case 
@Mock – Instead of mocking the object traditionally. This annotation directly mocks the declared object 
@Spy – To Spy on the existing instance 
@Before– For initialization purposes i.e. initializing the object before use 
Generic syntax for mocking –
For mocking the method calls –methodPowerMockito.when(object.printMessage(Mockito.anyString())).thenReturn(message);The above mocking will return a message when the print message method gets called with any string.
In the case of static methods, use like below:
1. For mocking the method calls –methodPowerMockito.when(object.printMessage(Mockito.anyString())).thenReturn(message);The above mocking will return a message when the print message method gets called with any string.
2. In the case of static methods, use like below:
PowerMockito.mockStatic(PropertiesReader.class);PowerMockito.when(PropertiesReader.getProperty(Constants.PAGE_SIZE)).thenReturn(ONE);
Here, getProperty(..) is a static method present inside the class-  PropertiesReaderSo static mock class before mocking its static method.
3.
PowerMockito.whenNew(ClassWithFinalMethods.class).withNoArguments().thenReturn(mockObject);
This will mock the new object creationi.e. when a new instance of type –  ClassWithFinalMethods is being generated, our mocked object –  ‘mockObject’ will get returned instead of the actual/real new object.
4.
Whitebox.setInternalState(mockVmReplication1, ID_PROPERTY, REPLICATION_ID1);
1. This will set the state/value of member variables inside the mocked objectIn above example, ID_PROPERTY of  mockVmReplication1 will get set with value =  REPLICATION_ID1
Call real methods to test using mocked objects –Real methods can be called from Junit test cases in 2 ways-
1. Using doCallRealMethod() –
Mockito.when(mockObject.methodUnderTest().thenCallRealMethod();
This will call actual method – methodUnderTest() when called using mocked object – mockObject.methodUndertest();
2. Using actual object creation
ClassUnderMock obj = new ClassUnderMock();obj.methodUnderTest();

Pros 
Cons 

doCallRealMethod don’t create real objects 
Actual object creation will create real objects which is not recommended in mocking 

Real object creation avoids setting member variables present inside the constructor 
Need to set the state of member variables which are getting set in the constructor 

Assert and Verify
Assert and Verify are used for checking/verifying the results of mocking.Assert class has multiple static methods that can be used for testing the results of mocking. Below are a few examples of the same:Assert.assertEquals – Fails when expected and actual values are not equalAssert.assertNotNull – Fails when the actual value is NULLAssert.assertNotEquals – Fails when expected and actual values are equalAssert.assertTrue – Fails when the actual value is falseAssert.assertFalse – Fails when the actual value is true 
Verify()- 

To ensure that the code satisfies all the required functionality, under all (or most of) the input combinations/values. 
To ensure that I can change the implementation and rely on JUnit test cases to tell me that all my functionality is still satisfied. 

Mockito.verify(mockedObject, Mockito.times(1)).methodUnderTest();
This will verify whether the methdUndertest() is called only 1s on mockedObjectIf not then execution fails with the below trace-“Wanted 2 times but was 1 time” 
In a Nutshell 
Unit testing is an essential practice in software development, ensuring each component functions as intended and meets quality standards. Mockito and PowerMockito are invaluable tools in this process. Mockito, with its simple and clean API, simplifies mocking and unit testing, while PowerMockito extends these capabilities to private, final, and static methods through Java Reflection API. Together, they empower developers to write effective, reliable tests, reducing dependencies and improving code quality. 
By leveraging these frameworks, developers can isolate and test individual components with precision, leading to early detection of issues and more robust software. Embracing Mockito and PowerMockito in your testing strategy can significantly enhance your development workflow, making your codebase more maintainable and reliable. Whether you are a seasoned developer or new to unit testing, incorporating these tools can streamline your testing process and lead to more efficient and effective software development. 
Calsoft offers state-of-the-art testing solutions by leveraging advanced testing techniques and tools. Our focus on accelerating customer journeys from QA to QE is unmatched.  
Talk to our Experts for more details 

Share this
Tags

Must-read

Mortgage Rates Could Fall Another Half Point Just from Market Normalization

It’s been a pretty good year so far for mortgage rates, which topped out at around 8% last year.The 30-year fixed is now priced...

Goldman Sachs loses profit after hits from GreenSky, real estate

Second-quarter profit fell 58% to $1.22 billion, or $3.08 a share, due to steep declines in trading and investment banking and losses related to...

Half of Japan’s chip-making equipment exports headed to China in Q1 · TechNode

Japan’s Ministry of Finance trade statistics show that half of Japan’s semiconductor manufacturing equipment exports were heading to China in the first quarter, according...
spot_img

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here