I want to write a test case for an override method that just calls another task, but doesn't return anything.
Here is one simple example:
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. Here we have two methods to test.
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 in src/test/java/com/example/myapplication folder.
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());
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.