單元測試框架 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
public void setUp() {
MockitoAnnotations.openMocks(this);
springContextAwareMockedStatic.when(() -> SpringContextAware.getBean(MessageSource.class)).thenReturn(messageSource);
}