相关疑难解决方法(0)

当我运行mockito测试时发生WrongTypeOfReturnValue Exception

错误细节:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
Boolean cannot be returned by updateItemAttributesByJuId()
updateItemAttributesByJuId() should return ResultRich
This exception might occur in wrongly written multi-threaded tests.
Please refer to Mockito FAQ on limitations of concurrency testing.
Run Code Online (Sandbox Code Playgroud)

我的代码:

@InjectMocks
protected ItemArrangeManager arrangeManagerSpy = spy(new ItemArrangeManagerImpl());
@Mock
protected JuItemWriteService juItemWriteService;

when(arrangeManagerSpy
    .updateItemAttributes(mapCaptor.capture(), eq(juId), eq(itemTO.getSellerId())))
    .thenReturn(false);
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我呼吁whenupdateItemAttributes(它返回boolean)不是updateItemAttributesByJuId.

  1. 为什么尝试的Mockito返回一个booleanupdateItemAttributesByJuId
  2. 怎么能纠正这个?

java mockito

75
推荐指数
6
解决办法
8万
查看次数

如何单元测试改装api调用?

我试图为每一块代码集成单元测试用例.但是我在为通过改造进行的api调用添加测试用例时遇到了问题.

JUnit编译器从不执行CallBack函数中的代码.

还有另一个选项是将所有api调用同步用于测试目的,但对于我的应用程序中的每个案例都不可能.

我该如何解决这个问题?我必须以任何方式在api调用中添加测试用例.

junit android unit-testing android-gradle-plugin retrofit

55
推荐指数
3
解决办法
3万
查看次数

Android RxJava 2 JUnit测试 - android.os.Looper中的getMainLooper没有模拟RuntimeException

我在尝试为正在使用的演示者运行JUnit测试时遇到RuntimeException observeOn(AndroidSchedulers.mainThread()).

由于它们是纯JUnit测试而不是Android检测测试,因此它们无法访问Android依赖项,导致我在执行测试时遇到以下错误:

java.lang.ExceptionInInitializerError
    at io.reactivex.android.schedulers.AndroidSchedulers$1.call(AndroidSchedulers.java:35)
    at io.reactivex.android.schedulers.AndroidSchedulers$1.call(AndroidSchedulers.java:33)
    at io.reactivex.android.plugins.RxAndroidPlugins.callRequireNonNull(RxAndroidPlugins.java:70)
    at io.reactivex.android.plugins.RxAndroidPlugins.initMainThreadScheduler(RxAndroidPlugins.java:40)
    at io.reactivex.android.schedulers.AndroidSchedulers.<clinit>(AndroidSchedulers.java:32)
    …
Caused by: java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked. See http://g.co/androidstudio/not-mocked for details.
    at android.os.Looper.getMainLooper(Looper.java)
    at io.reactivex.android.schedulers.AndroidSchedulers$MainHolder.<clinit>(AndroidSchedulers.java:29)
    ...


java.lang.NoClassDefFoundError: Could not initialize class io.reactivex.android.schedulers.AndroidSchedulers
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    …
Run Code Online (Sandbox Code Playgroud)

junit android unit-testing rx-java rx-java2

48
推荐指数
5
解决办法
2万
查看次数