单元测试框架 mockito
1、在项目的 pom.xml 引入 mockito 的依赖:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
2、模拟 Spring 的消息源对象:
@Mock
MessageSource messageSource;
3、模拟静态的 Spring 上下文感知对象:
static MockedStatic<SpringContextAware> springContextAwareMockedStatic = mockStatic(SpringContextAware.class);
4、在前置条件方法中模拟获取 Spring 的消息源对象:
@Before标签:框架,Spring,mockito,单元测试,springContextAwareMockedStatic,messageSource,SpringContex From: https://www.cnblogs.com/hapday/p/18352484
public void setUp() {
MockitoAnnotations.openMocks(this);
springContextAwareMockedStatic.when(() -> SpringContextAware.getBean(MessageSource.class)).thenReturn(messageSource);
}