gpt4 book ai didi

android - 如何使用 Mockito 在 android 中模拟 EditText

转载 作者:行者123 更新时间:2023-12-04 23:53:50 26 4
gpt4 key购买 nike

我正在尝试为我的 android 应用程序的验证程序编写一个单元测试。验证器接受参数 EditText,因此我需要模拟它。然而,模拟不起作用,迫使 Test 在调用 when() 方法时崩溃,但出现以下异常:

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.

我的代码是:

@RunWith(MockitoJUnitRunner.class)
public class MyUnitTest
{

@Mock
Context mMockContext;

@Test
public void validateIsCorrect() {
final EditText input = Mockito.mock(EditText.class);
when(input.getText()).thenReturn(Editable.Factory.getInstance().newEditable("123"));

...
}

}

build.gradle 文件中的依赖是:

 testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'

EditText 的方法getText() 不是privatefinal。我究竟做错了什么?是否可以通过这种方式模拟 EditText?怎么办?

最佳答案

当您运行单元测试时,您使用的是标准 JVM 上下文,而不是 Android 的上下文,这就是它崩溃的原因:Editable.Factory 类及其方法(如 getInstance()) 不在类路径中。他们也没有被 mock 过。我要做的是创建一个 implements Editable 的类,其中包含一个 private 成员以保存字符串引用并使用它来模拟 getText()方法。像这样的:

class MockEditable implements Editable {

private String str;
public MockEditable(String str) {
this.str = str;
}

@Override @NonNull
public String toString() {
return str;
}

@Override
public int length() {
return str.length();
}

@Override
public char charAt(int i) {
return str.charAt(i);
}

@Override
public CharSequence subSequence(int i, int i1) {
return str.subSequence(i, i1);
}

@Override
public Editable replace(int i, int i1, CharSequence charSequence, int i2, int i3) {
return this;
}

@Override
public Editable replace(int i, int i1, CharSequence charSequence) {
return this;
}

@Override
public Editable insert(int i, CharSequence charSequence, int i1, int i2) {
return this;
}

@Override
public Editable insert(int i, CharSequence charSequence) {
return this;
}

@Override
public Editable delete(int i, int i1) {
return this;
}

@Override
public Editable append(CharSequence charSequence) {
return this;
}

@Override
public Editable append(CharSequence charSequence, int i, int i1) {
return this;
}

@Override
public Editable append(char c) {
return this;
}

@Override
public void clear() {
}

@Override
public void clearSpans() {
}

@Override
public void setFilters(InputFilter[] inputFilters) {
}

@Override
public InputFilter[] getFilters() {
return new InputFilter[0];
}

@Override
public void getChars(int i, int i1, char[] chars, int i2) {
}

@Override
public void setSpan(Object o, int i, int i1, int i2) {
}

@Override
public void removeSpan(Object o) {
}

@Override
public <T> T[] getSpans(int i, int i1, Class<T> aClass) {
return null;
}

@Override
public int getSpanStart(Object o) {
return 0;
}

@Override
public int getSpanEnd(Object o) {
return 0;
}

@Override
public int getSpanFlags(Object o) {
return 0;
}

@Override
public int nextSpanTransition(int i, int i1, Class aClass) {
return 0;
}
}

然后你可以使用这个类

Mockito.when(input.getText()).thenReturn(new MockEditable("123"));

关于android - 如何使用 Mockito 在 android 中模拟 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41783464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com