'Spring boot Mockito mocking method from imported class
I'm fairly new to Mockito and I have been developing many test cases lately, like a lot. I now have a better understanding of mocking and stubbing.
But now I have this problem, which I'm sure I can't find the answer because of the words I'm using, and the results it gives because of the word "import", still I have tried hard to find a solution to my problem without luck, so I'm going to ask.
I have this class, which uses import, like all classes, but one method is being using directly from the imports.
Having the following class:
package package;
import another.package.OutsideUtils;
@Service
public class ClassBeingTested implements anotherClass {
@Override
public void MethodBeingTested () {
if(OutsideUtils.getBoolean()) {
//do stuff
}
}
}
And in my test class
package testpackage;
@ExtendWith(MockingExtension.class)
class ClassBeingTestedTest {
@InjectMocks
ClassBeingTested classBeingTested;
@Test
void whenMethodBeingTeste_ReturnTrue() {
when(OutsideUtils.getBoolean()).thenReturn(true);
classBeingTested.MethodBeingTested();
}
@Test
void whenMethodBeingTeste_ReturnFalse() {
when(OutsideUtils.getBoolean()).thenReturn(false);
classBeingTested.MethodBeingTested();
}
}
This is just to exemplify what I need, I want to know if it is possible to do the when(OutsideUtils.getBoolean()).thenReturn(true) (or return false), and if not, what's the way to achieve this doing a minimun refactor in my class being tested.
Also using only mockito, no powermockito please.
Solution 1:[1]
Yes it is, here it is how:
In that case you are testing trying to use the when() method with a static method from that OutsideUtils, it won't work because the when() method requires a method from a mock.
Here is a valid usage of that when() method.
@ExtendWith(MockitoExtension.class)
public class ItemServiceTest {
@InjectMocks
private ItemService itemServiceMock;
@Mock
private ItemRepository itemRepositoryMock;
@Test
public void fetchItems_basic() {
// arrange
List<Item> items = Arrays.asList(new Item(1, "first", 10, 100), new Item(2, "second", 20, 200));
Integer expectedResultFirst = 1000;
Integer expectedResultSecond = 4000;
when(itemRepositoryMock.findAll()).thenReturn(items);
// act
List<Item> actualResult = itemServiceMock.fetchItems();
// assert
assertEquals(expectedResultFirst, actualResult.get(0).getValue());
assertEquals(expectedResultSecond, actualResult.get(1).getValue());
}
}
The solution for your example.
Since the latest versions of mockito you can mock static methods. You just need to add a dependency to your pom.xml.
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.8.0</version>
<scope>test</scope>
</dependency>
You will have to use mockStatic() method to create a MockedStatic, and use the when() method from the MockedStatic class, and then the logic is basically the same as any other usage of unit tests.
@ExtendWith(MockitoExtension.class)
class ClassBeingTestedTest {
@InjectMocks
private ClassBeingTested classBeingTested;
@Test
void whenMethodBeingTeste_ReturnTrue() {
// arrange
MockedStatic<OutsideUtils> utilities = Mockito.mockStatic(OutsideUtils.class);
utilities.when(OutsideUtils::getBoolean).thenReturn(true);
// act
Boolean result = classBeingTested.methodBeingTested();
// assert
assertEquals(true, result);
}
@Test
void whenMethodBeingTeste_ReturnFalse() {
// arrange
MockedStatic<OutsideUtils> utilities = Mockito.mockStatic(OutsideUtils.class);
utilities.when(OutsideUtils::getBoolean).thenReturn(false);
// act
Boolean result = classBeingTested.methodBeingTested();
// assert
assertEquals(false, result);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pedro Luiz |
