[英]How to write simple test cases for testing a Login Api in android?
[英]How to write test cases for Callbacks in android?
我想为只调用另一个任务但不返回任何内容的覆盖方法编写一个测试用例。
这是一个简单的例子:
build.gradle:
testImplementation 'junit:junit:4.12'
testImplementation 'org.powermock:powermock-module-junit4:1.6.6'
testImplementation 'org.powermock:powermock-module-junit4-rule:1.6.6'
testImplementation 'org.powermock:powermock-api-mockito:1.6.6'
testImplementation 'org.powermock:powermock-classloading-xstream:1.6.6'
testImplementation 'org.robolectric:robolectric:3.3.2'
testImplementation "org.robolectric:robolectric-annotations:3.3.2"
testImplementation "org.robolectric:shadows-support-v4:3.3.2"
AppUtils.java。 这里我们有两种方法来测试。
package com.example.myapplication.utils;
import android.content.Context;
import android.util.Log;
public class AppUtils {
public interface TResultRunnable <T>{
public void onSuccess(T result);
public void onFail(Exception e);
}
public static void parseIntWithCallback(String value, TResultRunnable<Integer> tResultRunnable ){
int intVal = 0;
try {
intVal = Integer.parseInt(value);
tResultRunnable.onSuccess(intVal);
} catch (Exception e) {
tResultRunnable.onFail(e);
}
}
public static void parseIntWithCallback2(Context context, String value, TResultRunnable<Integer> tResultRunnable ){
Log.i("test", "method begin. package="+context.getPackageName());
int intVal = 0;
try {
intVal = Integer.parseInt(value);
tResultRunnable.onSuccess(intVal);
Log.i("test", "method success");
} catch (Exception e) {
Log.e("test", "method exception "+e);
tResultRunnable.onFail(e);
}
}
}
ExampleUnitTest.java 在 src/test/java/com/example/myapplication 文件夹中。
package com.example.myapplication;
import android.content.Context;
import android.util.Log;
import com.example.myapplication.utils.AppUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Mockito.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest({AppUtils.class, Log.class})
public class ExampleUnitTest {
@Before
public void setUp() {
PowerMockito.mockStatic(Log.class);
}
@Test
public void test_parseInt() {
{
AppUtils.TResultRunnable<Integer> mockListener = mock(AppUtils.TResultRunnable.class);
AppUtils.parseIntWithCallback("1000", mockListener);
verify(mockListener, never()).onFail(any(Exception.class));
verify(mockListener, times(1)).onSuccess(1000);
}
{
AppUtils.TResultRunnable<Integer> mockListener = mock(AppUtils.TResultRunnable.class);
AppUtils.parseIntWithCallback("xxx", mockListener);
verify(mockListener, times(1)).onFail(any(Exception.class));
verify(mockListener, never()).onSuccess(anyInt());
}
}
@Test
public void test_parseIntWithCallback2() {
Context context = mock(Context.class);
{
AppUtils.TResultRunnable<Integer> mockListener = mock(AppUtils.TResultRunnable.class);
AppUtils.parseIntWithCallback2(context, "1000", mockListener);
verify(mockListener, never()).onFail(any(Exception.class));
verify(mockListener, times(1)).onSuccess(1000);
}
{
AppUtils.TResultRunnable<Integer> mockListener = mock(AppUtils.TResultRunnable.class);
AppUtils.parseIntWithCallback2(context, "xxx", mockListener);
verify(mockListener, times(1)).onFail(any(Exception.class));
verify(mockListener, never()).onSuccess(anyInt());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.