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)