我刚刚升级了我们的AEM代码库以使用JUnit 5,但我们在一些模拟LanguageManager特定方法的测试中遇到了一些问题。
@MockitoSettings(strictness = Strictness.LENIENT)
@ExtendWith({AemContextExtension.class, MockitoExtension.class})
public class SomeComponentTest {
@Mock
private LanguageManager languageManager;
@BeforeEach
public void setUp() throws Exception {
...
context.registerService(LanguageManager.class, languageManager);
...
}
@Test
public void someTest() {
...
Page mockPage = mock(Page.class);
given(languageManager.getLanguageRoot(any())).willReturn(mockPage);
...
}
}
即使我注册了LanguageManager mock,它似乎仍然得到了WCM IO提供的mock,该mock在这里初始化:https://github.com/wcm-io/wcm-io-testing/blob/develop/aem-mock/core/src/main/java/io/wcm/testing/mock/aem/context/AemContextImpl.java->registerInjectActivateService(新的MockLanguageManager(((;
我在这里做错了什么吗?或者根本没有办法监视/嘲笑LanguageManager的方法。请注意,在过去,当使用时,这是有效的
@RunWith(MockitoJUnitRunner.class)
我在这里得到了答案:https://wcm-io.atlassian.net/browse/WTES-58?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=21269#comment-21269
context.registerService(LanguageManager.class, myCustomLanguageManager,
Constants.SERVICE_RANKING, 100);
或
this.mockLanguageManager = context.registerService(LanguageManager.class,
spy((MockLanguageManager)context.getService(LanguageManager.class)),
Constants.SERVICE_RANKING, Integer.MAX_VALUE);
// Now you can leverage the mocked language manager that is provided, while
you can still override some of the methods if desired, due to it being a spy