簡體   English   中英

如何驗證使用確切參數調用靜態方法?

[英]How to verify that static method was invoked with exact arguments?

我有以下測試方法:

public static Map<String, CrxEntity> getCrxEntitiesByMixin(Session readSession, String rootPath, SupportedLocale locale, String mixin, String idPropertyName, Set<String> propertyNamesToStore) throws RepositoryException {
        return getCrxEntitiesByMixin(readSession, rootPath, locale, mixin, idPropertyName, propertyNamesToStore, null, false);
    }

下面的方法調用另一個公共方法,因此我應該只測試正確傳遞的參數。

我寫了以下代碼:

    @Test
    public void getCrxEntitiesByMixinTest() throws RepositoryException {
        PowerMockito.mockStatic(StaticUtils.class);

        when(StaticUtils.getCrxEntitiesByMixin(any(Session.class),anyString(),any(SupportedLocale.class),anyString(),anyString(),anySet(),anyList(),anyBoolean())).thenReturn(null);

        StaticUtils.getCrxEntitiesByMixin(sessionMock, "rootPath", SupportedLocale.EN, "mixin", "idPropName", Sets.<String>newHashSet());

        verify(StaticUtils.getCrxEntitiesByMixin(eq(sessionMock), eq("rootPath"), eq(SupportedLocale.EN), eq("mixin"), eq("idPropName"), eq(Sets.<String>newHashSet()), eq(Lists.<String>newArrayList()), eq(false)));
    }

我看到以下錯誤:

org.mockito.exceptions.misusing.NullInsteadOfMockException: 
Argument passed to verify() should be a mock but is null!
Examples of correct verifications:
    verify(mock).someMethod();
    verify(mock, times(10)).someMethod();
    verify(mock, atLeastOnce()).someMethod();
Also, if you use @Mock annotation don't miss initMocks()

如何解決我的問題?

我找到了答案。 Powermockito具有非常令人困惑的語法:

PowerMockito.verifyStatic();
StaticUtils.getCrxEntitiesByMixin(eq(sessionMock), eq("rootPath"), eq(SupportedLocale.EN), eq("mixin"), eq("idPropName"), eq(Sets.<String>newHashSet()), eq((List<String>)null), eq(false));

完整示例: https//gist.github.com/ThoMo/3916072

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM