Unit Testing with Mockito/PowerMockito

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.mockito
mockito-all
1.9.5
test
org.powermock
powermock-api-mockito
1.6.2
test
org.powermock
powermock-module-junit4
1.6.2
test

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 to
For 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-  PropertiesReader
So static mock class before mocking its static method.

3.

PowerMockito.whenNew(ClassWithFinalMethods.class).withNoArguments().thenReturn(mockObject);

This will mock the new object creation
i.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 object
In 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 equal
Assert.assertNotNull – Fails when the actual value is NULL
Assert.assertNotEquals – Fails when expected and actual values are equal
Assert.assertTrue – Fails when the actual value is false
Assert.assertFalse – Fails when the actual value is true 

Verify()- 

  1. To ensure that the code satisfies all the required functionality, under all (or most of) the input combinations/values. 
  2. 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 mockedObject
If 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.  

 
Share:

Related Posts

Anomaly Detection in Machine Learning Classification Algorithms vs Anomaly Detection

Anomaly Detection in Machine Learning: Classification Algorithms vs Anomaly Detection

Discover the power of anomaly detection in machine learning to enhance operational efficiency, reduce costs, and mitigate risks with the right algorithms and features.

Share:
How to Run Commands on Remote Windows Machine Using Python Paramiko Module

How to Run Commands on Remote Windows Machine Using Python Paramiko Module

Discover how Python’s Paramiko module simplifies remote command execution on Windows machines. Enhance security and productivity with Calsoft’s expert insights.

Share:

Importance of High Availability Feature in Storage

Discover the importance of High Availability (HA) in storage solutions. Learn how HA ensures data access, scalability, and operational efficiency in the IT industry.

Share:

7 Useful ServiceNow Integrations

ServiceNow has created its position as one of the best platforms and workflow integration tools. It enables enterprises to develop custom plug-ins as well to meet their unique business requirements.

Share:
GoLang Memory Management

GoLang Memory Management

One, created at Google and growing rapidly now having around 90,000+ repositories. Go can be used for cloud and systems programming and extended to the game server development and handling text-processing problems.

Share:
Understanding Desktop Virtualization VDI vs RDS

Virtual Desktop Infrastructure (VDI) Vs. Remote Desktop Services (RDS)

Both RDS and VDI are core components of desktop virtualization, and they satisfy specific computing requirements and scenarios with deployment readiness and flexibility. VDI and RDS have peculiarities that adapt to the different needs of a business, but making a choice between them could be difficult for some companies. Benefits of Virtual Desktop

Share: