あなたのAndroidコードのテストを実行する2つのオプションがあります。最初に、システムにadbを接続することによってコードをテストできるインストルメント済みのテストオプションがあります。
第2のより機能的な方法は、JUnitテストです。ここでは、Javaクラスだけがテストされ、他のすべてのAndroid関連のものが侮られます。
使用PowerMockito
あなたのクラス名の上にこれを追加し、他のCUTクラス名(テスト対象のクラス)
@RunWith(PowerMockRunner.class)
@PrepareForTest({TextUtils.class})
public class ContactUtilsTest
{
が含まれ、このあなたの@Beforeに
@Before
public void setup(){
PowerMockito.mockStatic(TextUtils.class);
mMyFragmentPresenter=new MyFragmentPresenterImpl();
}
これを追加PowerMockitoは、TextUtils内のメソッドのデフォルト値を返します。
たとえばのは、あなたの実装がで、その文字列が空であるかどうかをチェックしたりされていないとしましょうあなたの@Test
when(TextUtils.isEmpty(any(CharSequence.class))).thenReturn(true);
//Here i call the method which uses TextUtils and check if it is returning true
assertTrue(MyFragmentPresenterImpl.checkUsingTextUtils("Fragment");
また、あなたが "でどういう関連のGradle depedencies
testCompile "org.powermock:powermock-module-junit4:1.6.2"
testCompile "org.powermock:powermock-module-junit4-rule:1.6.2"
testCompile "org.powermock:powermock-api-mockito:1.6.2"
testCompile "org.powermock:powermock-classloading-xstream:1.6.2"
を追加する必要がありますより良い "と"最高 "ここ? –
基本的には、エミュレータでアプリケーションを実行せずにテストケースを作成したいですか? – user2511882