android - How do I test this Custom UI Class to have code coverage
问题描述
I made a custom UI class EzCustomEditTextEmail
by extending TextInputEditText
and have written the test class (EmailUITest
) for the same in android(test) package. I am trying to achieve code coverage for the Custom UI class and used Mockito for the same . write now what I am trying is to verify the line setMaxLines(1)
in the inIt()
method of EzCustomEditTextEmail
class and getting NullPointerException at the constructor
at super(context)
import android.content.Context;
import android.support.design.widget.TextInputEditText;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Patterns;
import android.view.inputmethod.EditorInfo;
public class EzCustomEditTextEmail extends TextInputEditText {
public EzCustomEditTextEmail(Context context) {
super(context);
inIt();
}
public EzCustomEditTextEmail(Context context, AttributeSet attrs) {
super(context, attrs);
inIt();
}
public EzCustomEditTextEmail(Context context, AttributeSet attrs, int defStyleAttrs) {
super(context, attrs, defStyleAttrs);
inIt();
}
private void inIt() {
setMaxLines(1);
setEditTextMaxLength(50);
setImeOptions(EditorInfo.IME_ACTION_NEXT);
setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
}
public void setEditTextMaxLength(int length) {
InputFilter[] filterArray = new InputFilter[1];
filterArray[0] = new InputFilter.LengthFilter(length);
setFilters(filterArray);
}
public boolean isValidEmail() {
return (!TextUtils.isEmpty(getText()) && !TextUtils.isEmpty(getText().toString().trim()) && Patterns.EMAIL_ADDRESS.matcher(getText().toString().trim()).matches());
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
if (text.length() > 0) {
if (!Patterns.EMAIL_ADDRESS.matcher(text).matches()) {
requestFocus();
setError("Invalid Email");
}
} else {
setError(null);
}
}
}
My test Class
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public class EmailUITest {
@Mock
EzCustomEditTextEmail ezCustomEditTextEmail;
@Mock
Context context;
// Initializing Mockito and setup Pre-Test
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
ezCustomEditTextEmail = new EzCustomEditTextEmail(context);
}
// Test Method
@Test
public void callVerifyNumberAPI() throws InterruptedException {
verify(ezCustomEditTextEmail, times(1)).setMaxLines(1);
}
}
I'm getting NullPointerException at the constructor
-> super(context)
解决方案
推荐阅读
- angular - 如何将来自 Observable 的异步数据返回从子组件传递到 Angular 中的父组件
- javascript - 使用jQuery触发锚标记的点击事件
- python - 从两个不同的文件并行调用 python 函数
- datepicker - 如何在 SwiftUI 中修改新的日期选择器?
- html - 带有自定义角的引导选项卡
- java - JPA 实体试图读取不存在/已删除的列
- python - aws lambda函数api网关文件上传
- android - 即使该插件已卸载,Android Studio 4.1 仍会显示插件不兼容错误
- mergesort - scilab中的合并排序
- html - 如何在 Bootstrap 中添加点击添加自动填充卡片?