android
アプリケーションのバリデーターにUnit Test
を書き込もうとしています。バリデーターはパラメータEditText
として受け入れますので、私はそれを嘲笑する必要があります。しかしモックは例外でwhen()
メソッドを呼び出すにクラッシュするTest
を強制的に動作しません:Mockitoを使用してアンドロイドでEditTextをモックする方法
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()
はprivate
またはではありません。私は間違って何をしていますか?このようにEditText
を模倣することは可能ですか?どうやって?
'EditText'クラス自体がクラスパスにどのようになるのでしょうか? – Divers
ユニットテストを実行するとアンドロイドが得られます。jarファイルをクラスパスに入れてテストを実行する必要があります。これは、 'EditText'クラスがそこにあるので、参照することができますが、メソッドの実装は空であり、使用時に実行時の例外がスローされるため、最初にそれらを使用するためにモックする必要があります。 – gcesarmza
私はそれを取得しません。なぜあなた自身の編集可能なものを作成する必要がありますか、ちょうどそれも嘲笑できませんでしたか? – GhostCat