gpt4 book ai didi

java - 在使用 junit 进行测试期间,Android 处理程序为空

转载 作者:搜寻专家 更新时间:2023-11-01 09:35:28 25 4
gpt4 key购买 nike

我正在尝试测试我的网络模块。当我在模拟器或设备上运行它时,处理程序没问题,但是当我尝试从测试中执行它时,不会调用 handler = null 和回调。我该如何解决这个问题?

public void performCall(Call callToPerform){
callToPerform.call.enqueue(new okhttp3.Callback() {
Handler handler = new Handler();

@Override
public void onFailure(okhttp3.Call call, IOException e) {
handler.post(() -> {
for (Callback callback : callToPerform.callbacks) {
callback.onFailure(callToPerform, e);
}
});
}

@Override
public void onResponse(okhttp3.Call call, final okhttp3.Response response){
handler.post(() -> {
for (Callback callback : callToPerform.callbacks) {
try {
callback.onResponse(callToPerform, new Response(response.body().bytes(), response.headers().toMultimap()));
} catch (IOException e) {
callback.onFailure(call, e);
}
}
});
}
});
}

我的 gradle 应用程序文件包含此参数。

testOptions {
unitTests.returnDefaultValues = true
}

最佳答案

好的,经过几个小时的研究,我找到了解决方案,它类似于 this :

package com.dpmedeiros.androidtestsupportlibrary;

import android.os.Handler;
import android.os.Looper;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static org.mockito.Mockito.*;

/**
* Utility methods that unit tests can use to do common android library mocking that might be needed.
*/
public class AndroidMockUtil {

private AndroidMockUtil() {}
/**
* Mocks main thread handler post() and postDelayed() for use in Android unit tests
*
* To use this:
* <ol>
* <li>Call this method in an {@literal @}Before method of your test.</li>
* <li>Place Looper.class in the {@literal @}PrepareForTest annotation before your test class.</li>
* <li>any class under test that needs to call {@code new Handler(Looper.getMainLooper())} should be placed
* in the {@literal @}PrepareForTest annotation as well.</li>
* </ol>
*
* @throws Exception
*/
public static void mockMainThreadHandler() throws Exception {
PowerMockito.mockStatic(Looper.class);
Looper mockMainThreadLooper = mock(Looper.class);
when(Looper.getMainLooper()).thenReturn(mockMainThreadLooper);
Handler mockMainThreadHandler = mock(Handler.class);
Answer<Boolean> handlerPostAnswer = new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
Runnable runnable = invocation.getArgumentAt(0, Runnable.class);
Long delay = 0L;
if (invocation.getArguments().length > 1) {
delay = invocation.getArgumentAt(1, Long.class);
}
if (runnable != null) {
mainThread.schedule(runnable, delay, TimeUnit.MILLISECONDS);
}
return true;
}
};
doAnswer(handlerPostAnswer).when(mockMainThreadHandler).post(any(Runnable.class));
doAnswer(handlerPostAnswer).when(mockMainThreadHandler).postDelayed(any(Runnable.class), anyLong());
PowerMockito.whenNew(Handler.class).withArguments(mockMainThreadLooper).thenReturn(mockMainThreadHandler);
}

private final static ScheduledExecutorService mainThread = Executors.newSingleThreadScheduledExecutor();

}

关于java - 在使用 junit 进行测试期间,Android 处理程序为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43597745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com