在安卓应用程序中,有一些地方使用android.util.Log记录一些数据,并希望验证数据是否已被记录。
例如,拥有静态助手函数printOutIntent,,它在内部调用Log.i()
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());
}
}
}在测试中,
@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()?
发布于 2020-12-15 13:23:20
找到一种方法(应该有更好的方法),如果类/数据是通过某些类提供的,则必须模拟它们。
@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));
}
}https://stackoverflow.com/questions/65202366
复制相似问题