基础配置:Module:app中增加相关配置

defaultConfig中增加配置testInstrumentationRunner配置:

0818b9ca8b590ca3270a3433284dd417.png

在Android目录下配置:

0818b9ca8b590ca3270a3433284dd417.png

在Dependencies目录下配置:

0818b9ca8b590ca3270a3433284dd417.png

单个类单元测试

单个类的单元测试比较简单,在类中右击选择Go To--->Test

public class JUnitBean {

public int sum(int a, int b) {

return a + b;

}

public int multiply(int a, int b) {

return a * b;

}

}

0818b9ca8b590ca3270a3433284dd417.png

如果以前有写过测试类会有相关选择,如果是第一次编写可以选择Create New Test,直接创建

0818b9ca8b590ca3270a3433284dd417.png

根据自己需要进行单元测试方法选择以及测试类的类名和包名等,点击Ok

0818b9ca8b590ca3270a3433284dd417.png

单个类的单元测试放在test目录中,如果没有这个包可以自己创建,不影响

0818b9ca8b590ca3270a3433284dd417.png

新建测试类之后再测试类中声明JUitBean对象,并在setUp方法中进行相关初始化,然后再测试方法中使用类调用相关方法进行测试

public class JUnitBeanTest {

private JUnitBean bean;

@Before

public void setUp() throws Exception {

//初始化对象

bean = new JUnitBean();

}

@Test

public void sum() throws Exception {

//测试sum方法

assertEquals(6,bean.sum(3,4));

}

@Test

public void multiply() throws Exception {

//测试multiply方法

assertEquals(6,bean.multiply(3,2));

}

}

测试结果,sum结果异常,multiply正常通过

0818b9ca8b590ca3270a3433284dd417.png

简单的

bean

对象测试已经通过

Activity及网络请求单元测试

接下来看看Activity相关测试,建立测试类和单个类建立测试类步骤一样(需要特别注意的是测试类貌似不支持private修饰的方法,

public和protected都可以),只是在测试类放的目录是在androidTest目录下

0818b9ca8b590ca3270a3433284dd417.png

建立好测试类之后实现方式有两种:

第一种是使用@RunWith(AndroidJUnit4.class)

@RunWith(AndroidJUnit4.class)

public class LoginActivityTest {

//当前Activity初始化

@Rule

public ActivityTestRule mActivityRule = new ActivityTestRule(LoginActivity.class);

public LoginActivity loginActivity;

private EditText etName;

private EditText etPassword;

private final Integer LOCK = 1;

@MainThread

@Test

public void testLogin() {

loginActivity = (LoginActivity) mActivityRule.getActivity();

loginActivity.mgr.clear();

//初始化控件

etName = (EditText) loginActivity.findViewById(R.id.login_phone_number);

etPassword = (EditText) loginActivity.findViewById(R.id.login_password);

//模拟人为向输入框中输入

onView(withId(R.id.login_phone_number)).perform(typeText("账号"), ViewActions.closeSoftKeyboard());

onView(withId(R.id.login_password)).perform(typeText("密码"), ViewActions.closeSoftKeyboard());

//进行网络请求

Map map = new HashMap<>();

map.put("phoneNo", etName.getText().toString());

map.put("password", MD5Util.md5(new StringBuilder().append(Constant.ACCESS_KEY).append(etPassword.getText().toString()).toString()).toUpperCase());

ApiCaller.call(loginActivity, new ApiCaller.MyStringCallback(loginActivity, true) {

@Override

public void onError(Call call, Exception e, int id) {

super.onError(call, e, id);

}

@Override

public void onResponse(String response, int id) {

super.onResponse(response, id);

if ("0000".equals(head.getResponseCode())) {

//网络请求完成后唤醒

synchronized (LOCK) {

LOCK.notify();

}

//进行相关业务处理

}

});

//网络请求是异步完成,所以在此等待

try {

synchronized (LOCK) {

LOCK.wait();

}

} catch (InterruptedException e) {

Assert.assertNotNull(e);

}

}

}

0818b9ca8b590ca3270a3433284dd417.png

第二种是继承ActivityInstrumentationTestCase2

public class LoginActivi2Test extends ActivityInstrumentationTestCase2 {

private LoginActivity mActivity;

private EditText etName;

private EditText etPassword;

public LoginActivi2Test() {

super(LoginActivity.class);

}

private final Integer LOCK = 1;

//初始化

@Before

public void setUp() throws Exception {

super.setUp();

injectInstrumentation(InstrumentationRegistry.getInstrumentation());

mActivity = this.getActivity();

//初始化控件

etName = (EditText) mActivity.findViewById(R.id.login_phone_number);

etPassword = (EditText) mActivity.findViewById(R.id.login_password);

}

@Test

public void testLogin2(){

mActivity.mgr.clear();

//仿键盘输入

onView(withId(R.id.login_phone_number)).perform(typeText("账号"), ViewActions.closeSoftKeyboard());

onView(withId(R.id.login_password)).perform(typeText("密码"), ViewActions.closeSoftKeyboard());

//参数设置进行网络请求

Map map = new HashMap<>();

map.put("phoneNo", etName.getText().toString());

map.put("password", etPassword.getText().toString());

ApiCaller.call(mActivity, new ApiCaller.MyStringCallback(mActivity, true){

@Override

public void onError(Call call, Exception e, int id) {

super.onError(call, e, id);

}

@Override

public void onResponse(String response, int id) {

super.onResponse(response, id);

if ("0000".equals(head.getResponseCode())) {

//网络请求完成后唤醒

synchronized (LOCK) {

LOCK.notify();

}

//进行相关业务处理

}

});

//网络请求是异步完成,所以在此等待

try {

synchronized (LOCK) {

LOCK.wait();

}

} catch (InterruptedException e) {

Assert.assertNotNull(e);

}

}

}

0818b9ca8b590ca3270a3433284dd417.png

经过测试两个方法都是可以进行单元测试并且可以通过测试,请多多指教

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐