首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何验证一个android.util.Log.i(a,b)被调用

如何验证一个android.util.Log.i(a,b)被调用
EN

Stack Overflow用户
提问于 2020-12-08 15:57:49
回答 1查看 26关注 0票数 0

在安卓应用程序中,有一些地方使用android.util.Log记录一些数据,并希望验证数据是否已被记录。

例如,拥有静态助手函数printOutIntent,,它在内部调用Log.i()

代码语言:javascript
复制
public class Utils{

    public static void printOutIntent(Intent intent, String TAG, boolean isDebug) {
        if (isDebug) {
            StringBuilder body = new StringBuilder();
            Bundle bundle = (intent != null) ? intent.getExtras() : new Bundle();
            if (bundle != null) {
                for (String key : bundle.keySet()) {
                    Object value = bundle.get(key);
                    body.append(key).append("=").append(value.toString()).append("\n");
                }
            }
            Log.i(TAG, body.toString());
        }
    }
}

在测试中,

代码语言:javascript
复制
@RunWith(RobolectricTestRunner.class)
public class TestTest {

   @Test
    public void test_logIntent() throws Exception {
        // setup
        final String others = "{\"key2-1\":\"val2-1\",\"key-2-2\":\"val2-2\"" +
                "}";
        Intent intent = new Intent();
        intent.putExtra("key", "test_val");
        intent.putExtra("key2", others);

        StringBuilder body = new StringBuilder();
        Bundle bundle = (intent != null) ? intent.getExtras() : new Bundle();
        if (bundle != null) {
            for (String key : bundle.keySet()) {
                Object value = bundle.get(key);
                body.append(key).append("=").append(value.toString()).append("\n");
            }
        }

        String dataStr = body.toString();

        ArgumentCaptor<String> requestCaptor = ArgumentCaptor.forClass(String.class);
        Utils mockedUtils = mock(Utils.class);
        android.util.Log theLog = mock(android.util.Log.class);

        mockedUtils.printOutIntent(intent, "TEST", true);

        verify(theLog, times(1)).i(eq("TEST"), requestCaptor.capture());
        String logStr = requestCaptor.getValue();
        assertEquals("log should be same.", dataStr, logStr);

    }
}

verify(theLog, times(1)).i(eq("TEST"), requestCaptor.capture());是成功的,但是requestCaptor.getValue()没有返回任何东西。

如何用正确的数据记录andriod.util.Log.i()?

EN

回答 1

Stack Overflow用户

发布于 2020-12-15 13:23:20

找到一种方法(应该有更好的方法),如果类/数据是通过某些类提供的,则必须模拟它们。

代码语言:javascript
复制
@RunWith(PowerMockRunner.class)
@PrepareForTest({Log.class,
        OtherData.class,
        Bundle.class,
        Intent.class}) {

@Test
public void test_logIntent() throws Exception {
        // setup
        final String others = "{\"key2-1\":\"val2-1\",\"key-2-2\":\"val2-2\"" +
        "}";

        Bundle bundleMock = mock(Bundle.class);
        when(bundleMock.keySet()).thenReturn(key);
        when(bundleMock.get("key1")).thenReturn("val1");
        when(bundleMock.get("key2")).thenReturn("val2");
        when(bundleMock.get("others")).thenReturn(others);

        Intent intentMock = mock(Intent.class);
        when(intentMock.getExtras()).thenReturn(bundleMock);
        
        // if there is other class which could return Intent in the code path, it needs to be mocked as well, i.e.
        /*
        OtherData otherDataMock = mock(OtherData.class);
        when(otherDataMock.toIntent()).thenReturn(intentMock);

        Intent intent = (otherDataMock != null) ? otherDataMock.toIntent() : null;
        */

        Intent intent = intentMock;

        StringBuilder body = new StringBuilder();
        Bundle bundle = (intent != null) ? intent.getExtras() : bundleMock;
        if (bundle != null) {
            for (String key : bundle.keySet()) {
                Object value = bundle.get(key);
                body.append(key).append("=").append(value.toString()).append("\n");
            }
        }

        String dataStr = body.toString();
        PowerMockito.mockStatic(Log.class);

        //test
        Utils.printOutIntent(intent, "TEST1", true);

        PowerMockito.verifyStatic(times(1));
        android.util.Log.i(eq("+++TEST1"), eq(dataStr));

        }
     }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65202366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档