. Advertisement .
..3..
. Advertisement .
..4..
Effective unit testing of Java applications is done using the mocking framework package mockito. Using Mockito, interfaces may be mocked so that dummy functionality can be created for use in unit experiment. In this blog, we will introduce “How to mock a final class with mockito“. Let’s read it!
What can be mocked with Mockito?
A mocking framework is called Mockito. It is a Java-based library used to develop straightforward and fundamental test APIs for carrying out unit testing of Java programs. Additionally, it is compatible with other frameworks like JUnit and TestNG. The technique of creating objects that serve as mocks or clones of the real things is known as mocking. In other words, mocking is a testing method that substitutes fake objects for real ones during testing. Mock objects respond to a specified (dummy) input by producing a certain (dummy) output.
You create a moke object of a class or interface by using the Mockito.mock() function. Then, you can check if its methods were called and stub out return values for them by using the mock. Let’s examine the following illustration:
@Test
public void givenCountMethodMocked_WhenCountInvoked_ThenMockedValueReturned() {
UserRepository localMockRepository = Mockito.mock(UserRepository.class);
Mockito.when(localMockRepository.count()).thenReturn(111L);
long userCount = localMockRepository.count();
Assert.assertEquals(111L, userCount);
Mockito.verify(localMockRepository).count();
}
Before using this procedure, there is nothing further you need to do to it. It may be used to build local mocks within a method as well as mock class fields.
How to mock a final class with mockito?
Option 1: Add the following to your Gradle file:
Mockito v2 is the only version that supports mocking final/static classes and functions.
Add the following to your Gradle file:
testImplementation 'org.mockito:mockito-inline:2.13.0'
According to the Mockito FAQ, this is not possible with Mockito v1:
What are the limitations of Mockito
- Needs java 1.5+
- Cannot mock final classes
…
Option 2: Build a concrete case and a mock case of your final class
Mocking a final class with mockito is as simple as any other class. Look at the following example:
@Test public void whenMockFinalClassMockWorks() {
FinalList finalList = new FinalList();
FinalList mock = mock(FinalList.class);
when(mock.size()).thenReturn(2);
assertNotEquals(mock.size(), finalList.size());
}
You build a concrete case and a mock case of your final class, mock a method, and then confirm that the mocked case behaves differently. This is like the test presented above.
Option 3: Make a resources directory in your test directory
Excepting the solutions mentionedd above, there is another for you to mock a final class with mockito. Let’s follow these steps:
If you have not already had a resources directory, let’s make one in your test directory. Make the directory mockito-extensions inside the resources, and place the file org.mockito.plugins.MockMaker there. src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker should be the full path to the file.
Then, let’s paste this text mock-maker-inline
in the file org.mockito.plugins.MockMaker
, the mock maker is now accessible during testing.
Option 4: Include this dependency to your pom
Another solution for you is including this dependency to your pom. Look at the following example:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>
Conclusion
Individual solutions provided in these tools are some of the most fundamental for anyone faced with the question “How to mock a final class with mockito“. If you still need assistance or have basic Python questions, a growing community of people are usually willing to assist you. In addition, we anticipate a more creative day filled with new ideas and code.
Read more
Leave a comment