Mockito
跳到导航
跳到搜索
测试调用类的静态方法的示例
需要先引入:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
单元测试代码:
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class MathUtilsTest {
@Test
public void testPrintSumMethodCalled() {
try (MockedStatic<MathUtils> mockedMathUtils = mockStatic(MathUtils.class)) {
// 模拟的调用场景,这里假设在某个业务逻辑代码中会调用MathUtils.printSum方法
// 例如以下是模拟调用
MathUtils.printSum(3, 5);
// 使用verify方法来验证MathUtils.printSum方法是否被调用,并且参数是否符合预期
mockedMathUtils.verify(() -> MathUtils.printSum(3, 5));
}
}
@Test
public void testPrintSumMethodCalledWithAnyParameters() {
try (MockedStatic<MathUtils> mockedMathUtils = mockStatic(MathUtils.class)) {
// 模拟的调用场景,这里假设在某个业务逻辑代码中会调用MathUtils.printSum方法
MathUtils.printSum(10, 20);
// 如果不关心具体传入的参数是什么,只想验证方法被调用了,可以使用anyInt()等参数匹配器(根据参数类型选择合适的匹配器)
mockedMathUtils.verify(() -> MathUtils.printSum(anyInt(), anyInt()));
}
}
}
package love.wishx.hd_volunteer_center.service;
import love.wishx.hd_volunteer_center.BaseTest;
import love.wishx.hd_volunteer_center.entity.Role;
import love.wishx.hd_volunteer_center.service.impl.MySubServiceImpl;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import java.util.ArrayList;
import java.util.List;
public class IMyServiceTest extends BaseTest {
@Autowired
private IMyService myService;
@MockBean
private MySubServiceImpl subService;
@Test
public void test() {
Answer<Role> answer = new Answer<Role>() {
@Override
public Role answer(InvocationOnMock invocation) throws Throwable {
String a = invocation.getArgument(0, String.class);
Object b = invocation.getArgument(1, Object.class);
String c = invocation.getArgument(2, String.class);
System.out.printf("a=%s\n", a);
System.out.printf("b=%s\n", b.toString());
System.out.printf("c=%s\n", c);
return (Role) invocation.callRealMethod();
}
};
Mockito.doAnswer(answer).when(subService).getRole(Mockito.anyString(), Mockito.any(), Mockito.anyString());
List<Role> roleList = new ArrayList<>();
Role tmp = new Role();
tmp.setName("123");
tmp.setId(2);
roleList.add(tmp);
Role r = myService.getRole("j", roleList, "k");
System.out.println(r);
}
}
package love.wishx.hd_volunteer_center.service;
import love.wishx.hd_volunteer_center.entity.Role;
public interface IMyService {
Role getRole(String a, Object b, String c);
}
package love.wishx.hd_volunteer_center.service.impl;
import love.wishx.hd_volunteer_center.entity.Role;
import love.wishx.hd_volunteer_center.service.IMyService;
import love.wishx.hd_volunteer_center.service.IMySubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MyServiceImpl implements IMyService {
@Autowired
private IMySubService subService;
@Override
public Role getRole(String a, Object b, String c) {
return subService.getRole(a, b, c);
}
}
package love.wishx.hd_volunteer_center.service;
import love.wishx.hd_volunteer_center.entity.Role;
public interface IMySubService {
Role getRole(String a, Object b, String c);
}
package love.wishx.hd_volunteer_center.service.impl;
import love.wishx.hd_volunteer_center.entity.Role;
import love.wishx.hd_volunteer_center.service.IMySubService;
import org.springframework.stereotype.Service;
@Service
public class MySubServiceImpl implements IMySubService {
@Override
public Role getRole(String a, Object b, String c) {
Role role = new Role();
role.setId(3);
role.setName("bbbb");
return role;
}
}