Paging の実装をテストする

アプリに Paging ライブラリを実装する際は、堅牢なテスト戦略と組み合わせる必要があります。PagingSourceRemoteMediator などのデータ読み込みコンポーネントをテストして、期待どおりに動作することを確認してください。また、エンドツーエンドのテストを作成して、Paging の実装のコンポーネントがすべて、予期しない副作用なしで正しく連携することも確認してください。

このガイドでは、アプリのさまざまなアーキテクチャ レイヤで Paging ライブラリをテストする方法と、Paging 実装全体のエンドツーエンドのテストを作成する方法について説明します。

UI レイヤのテスト

Paging ライブラリで取得されたデータは、UI で Flow<PagingData<Value>> として使用されます。UI 内のデータが想定どおりであることを確認するためのテストの作成には、paging-testing の依存関係を含めます。Flow<PagingData<Value>>asSnapshot() 拡張機能が含まれています。ラムダ レシーバーに、スクロール操作のモックを可能にする API が用意されています。モックされたスクロール操作によって生成された標準の List<Value> を返します。これにより、ページングされたデータに、その操作によって生成された想定される要素が含まれていることをアサートできます。これを次のスニペットに示します。

fun test_items_contain_one_to_ten() = runTest {
 
// Get the Flow of PagingData from the ViewModel under test
 
val items: Flow<PagingData<String>> = viewModel.items

 
val itemsSnapshot: List<String> = items.asSnapshot {
   
// Scroll to the 50th item in the list. This will also suspend till
   
// the prefetch requirement is met if there's one.
   
// It also suspends until all loading is complete.
    scrollTo
(index = 50)
 
}

 
// With the asSnapshot complete, you can now verify that the snapshot
 
// has the expected values
  assertEquals
(
    expected
= (0..50).map(Int::toString),
    actual
= itemsSnapshot
 
)
}

また、以下のスニペットに示すように、特定の述語が満たされるまでスクロールすることもできます。


fun test_footer_is_visible() = runTest {
 
// Get the Flow of PagingData from the ViewModel under test
 
val items: Flow<PagingData<String>> = viewModel.items

 
val itemsSnapshot: List<String> = items.asSnapshot {
   
// Scroll till the footer is visible
    appendScrollWhile
{  item: String -> item != "Footer" }
 
}

変換をテストする

PagingData ストリームに適用する変換を対象とする単体テストも作成する必要があります。asPagingSourceFactory 拡張機能を使用します。この拡張機能は、次のデータ型で使用できます。

  • List<Value>
  • Flow<List<Value>>

使用する拡張機能は、テストする内容に応じて選択します。次の形式で入力してください。

  • List<Value>.asPagingSourceFactory(): データで map()insertSeparators() などの静的変換をテストする場合。
  • Flow<List<Value>>.asPagingSourceFactory(): バッキング データソースへの書き込みなど、データの更新がページング パイプラインにどのように影響するかをテストする場合。

これらの拡張機能を使用するには、次のパターンを使用します。

  • 必要に応じて適切な拡張機能を使用して、PagingSourceFactory を作成します。
  • Repositoryフェイクで、返された PagingSourceFactory を使用します。
  • その RepositoryViewModel に渡します。

その後、前のセクションで説明したように ViewModel をテストできます。 次の ViewModel について考えてみましょう。

class MyViewModel(
  myRepository
: myRepository
) {
 
val items = Pager(
    config
: PagingConfig,
    initialKey
= null,
    pagingSourceFactory
= { myRepository.pagingSource() }
 
)
 
.flow
 
.map { pagingData ->
    pagingData
.insertSeparators<String, String> { before, _ ->
     
when {
       
// Add a dashed String separator if the prior item is a multiple of 10
        before
.last() == '0' -> "---------"
       
// Return null to avoid adding a separator between two items.
       
else -> null
     
}
 
}
}

MyViewModel で変換をテストするには、次のスニペットに示すように、変換するデータを表す静的 List に委任する MyRepository の疑似インスタンスを与えます。

class FakeMyRepository(): MyRepository {
   
private val items = (0..100).map(Any::toString)

   
private val pagingSourceFactory = items.asPagingSourceFactory()

   
val pagingSource = pagingSourceFactory()
}

その後、次のスニペットに示すように、セパレータ ロジックのテストを作成できます。

fun test_separators_are_added_every_10_items() = runTest {
 
// Create your ViewModel
 
val viewModel = MyViewModel(
    myRepository
= FakeMyRepository()
 
)
 
// Get the Flow of PagingData from the ViewModel with the separator transformations applied
 
val items: Flow<PagingData<String>> = viewModel.items
                 
 
val snapshot: List<String> = items.asSnapshot()

 
// With the asSnapshot complete, you can now verify that the snapshot
 
// has the expected separators.
}

データレイヤのテスト

データレイヤのコンポーネントの単体テストを作成して、データソースからのデータが適切に読み込まれることを確認します。依存関係の疑似バージョンを用意して、テスト対象のコンポーネントが独立して正しく機能することを確認します。リポジトリ レイヤでテストする必要がある主なコンポーネントは、PagingSourceRemoteMediator です。以降のセクションの例は、ネットワークを使用したページングのサンプルに基づいています。

PagingSource のテスト

PagingSource 実装の単体テストでは、TestPager を使用して PagingSource インスタンスを設定し、そこからデータを読み込みます。

テスト用の PagingSource インスタンスを設定するには、架空のデータをコンストラクタに渡します。これにより、テストのデータを制御できます。次の例では、RedditApi パラメータは Retrofit インターフェースを使用して、サーバー リクエストとレスポンス クラスを定義します。 疑似バージョンはインターフェースを実装し、必要な関数をオーバーライドして、テストで疑似オブジェクトがどのように反応するかを設定する便利なメソッドを提供します。

フェイクの準備ができたら、依存関係を設定し、テストで PagingSource オブジェクトを初期化します。次の例は、テスト投稿のリストを使用して FakeRedditApi オブジェクトを初期化し、RedditPagingSource インスタンスをテストする方法を示しています。

class SubredditPagingSourceTest {
 
private val mockPosts = listOf(
    postFactory
.createRedditPost(DEFAULT_SUBREDDIT),
    postFactory
.createRedditPost(DEFAULT_SUBREDDIT),
    postFactory
.createRedditPost(DEFAULT_SUBREDDIT)
 
)
 
private val fakeApi = FakeRedditApi().apply {
    mockPosts
.forEach { post -> addPost(post) }
 
}

 
@Test
 
fun loadReturnsPageWhenOnSuccessfulLoadOfItemKeyedData() = runTest {
   
val pagingSource = RedditPagingSource(
      fakeApi
,
      DEFAULT
_SUBREDDIT
   
)

   
val pager = TestPager(CONFIG, pagingSource)

   
val result = pager.refresh() as LoadResult.Page

   
// Write assertions against the loaded data
    assertThat
(result.data)
   
.containsExactlyElementsIn(mockPosts)
   
.inOrder()
 
}
}

TestPager を使用すると、次のこともできます。

  • PagingSource からの連続読み込みをテストします。
    @Test
   
fun test_consecutive_loads() = runTest {

     
val page = with(pager) {
        refresh
()
        append
()
        append
()
     
} as LoadResult.Page

      assertThat
(page.data)
     
.containsExactlyElementsIn(testPosts)
     
.inOrder()
   
}
  • PagingSource でエラーのシナリオをテストします。
    @Test
   
fun refresh_returnError() {
       
val pagingSource = RedditPagingSource(
          fakeApi
,
          DEFAULT
_SUBREDDIT
       
)
       
// Configure your fake to return errors
        fakeApi
.setReturnsError()
       
val pager = TestPager(CONFIG, source)

        runTest
{
            source
.errorNextLoad = true
           
val result = pager.refresh()
            assertTrue
(result is LoadResult.Error)

           
val page = pager.getLastLoadedPage()
            assertThat
(page).isNull()
       
}
   
}

RemoteMediator のテスト

RemoteMediator 単体テストの目的は、load() 関数が正しい MediatorResult を返すことを確認することです。データベースに挿入されるデータなどの副作用のテストは、統合テストに適しています。

最初のステップは、RemoteMediator 実装に必要な依存関係を判断することです。次の例は、Room データベース、Retrofit インターフェース、検索文字列を必要とする RemoteMediator 実装を示しています。

KotlinJavaJava
@OptIn(ExperimentalPagingApi::class)
class PageKeyedRemoteMediator(
 
private val db: RedditDb,
 
private val redditApi: RedditApi,
 
private val subredditName: String
) : RemoteMediator<Int, RedditPost>() {
 
...
}
public class PageKeyedRemoteMediator
 
extends RxRemoteMediator<Integer, RedditPost> {

 
@NonNull
 
private RedditDb db;
 
@NonNull
 
private RedditPostDao postDao;
 
@NonNull
 
private SubredditRemoteKeyDao remoteKeyDao;
 
@NonNull
 
private RedditApi redditApi;
 
@NonNull
 
private String subredditName;

 
public PageKeyedRemoteMediator(
   
@NonNull RedditDb db,
   
@NonNull RedditApi redditApi,
   
@NonNull String subredditName
 
) {
     
this.db = db;
     
this.postDao = db.posts();
     
this.remoteKeyDao = db.remoteKeys();
     
this.redditApi = redditApi;
     
this.subredditName = subredditName;
     
...
 
}
}
public class PageKeyedRemoteMediator
 
extends ListenableFutureRemoteMediator<Integer, RedditPost> {

 
@NonNull
 
private RedditDb db;
 
@NonNull
 
private RedditPostDao postDao;
 
@NonNull
 
private SubredditRemoteKeyDao remoteKeyDao;
 
@NonNull
 
private RedditApi redditApi;
 
@NonNull
 
private String subredditName;
 
@NonNull
 
private Executor bgExecutor;

 
public PageKeyedRemoteMediator(
   
@NonNull RedditDb db,
   
@NonNull RedditApi redditApi,
   
@NonNull String subredditName,
   
@NonNull Executor bgExecutor
 
) {
   
this.db = db;
   
this.postDao = db.posts();
   
this.remoteKeyDao = db.remoteKeys();
   
this.redditApi = redditApi;
   
this.subredditName = subredditName;
   
this.bgExecutor = bgExecutor;
   
...
 
}
}

PagingSource のテスト セクションに示すように、Retrofit インターフェースと検索文字列を指定できます。Room データベースのモック バージョンを提供することは非常に複雑です。そのため完全なモック バージョンではなく、データベースのインメモリ実装を提供する方が簡単です。Room データベースを作成するには Context オブジェクトが必要なため、この RemoteMediator テストを androidTest ディレクトリに配置し、AndroidJUnit4 テストランナーで実行して、テストアプリのコンテキストにアクセスできるようにする必要があります。インストルメント化テストの詳細については、インストルメント化単体テストを作成するをご覧ください。

状態がテスト関数間でリークしないようにティアダウン関数を定義します。これにより、テスト実行間で一貫した結果が得られます。

KotlinJavaJava
@ExperimentalPagingApi
@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidJUnit4::class)
class PageKeyedRemoteMediatorTest {
 
private val postFactory = PostFactory()
 
private val mockPosts = listOf(
    postFactory
.createRedditPost(SubRedditViewModel.DEFAULT_SUBREDDIT),
    postFactory
.createRedditPost(SubRedditViewModel.DEFAULT_SUBREDDIT),
    postFactory
.createRedditPost(SubRedditViewModel.DEFAULT_SUBREDDIT)
 
)
 
private val mockApi = mockRedditApi()

 
private val mockDb = RedditDb.create(
   
ApplicationProvider.getApplicationContext(),
    useInMemory
= true
 
)

 
@After
 
fun tearDown() {
    mockDb
.clearAllTables()
   
// Clear out failure message to default to the successful response.
    mockApi
.failureMsg = null
   
// Clear out posts after each test run.
    mockApi
.clearPosts()
 
}
}
@RunWith(AndroidJUnit4.class)
public class PageKeyedRemoteMediatorTest {
 
static PostFactory postFactory = new PostFactory();
 
static List<RedditPost> mockPosts = new ArrayList<>();
 
static MockRedditApi mockApi = new MockRedditApi();
 
private RedditDb mockDb = RedditDb.Companion.create(
   
ApplicationProvider.getApplicationContext(),
   
true
 
);

 
static {
   
for (int i=0; i<3; i++) {
     
RedditPost post = postFactory.createRedditPost(DEFAULT_SUBREDDIT);
      mockPosts
.add(post);
   
}
 
}

 
@After
 
public void tearDown() {
    mockDb
.clearAllTables();
   
// Clear the failure message after each test run.
    mockApi
.setFailureMsg(null);
   
// Clear out posts after each test run.
    mockApi
.clearPosts();
 
}
}
@RunWith(AndroidJUnit4.class)
public class PageKeyedRemoteMediatorTest {
 
static PostFactory postFactory = new PostFactory();
 
static List<RedditPost> mockPosts = new ArrayList<>();
 
static MockRedditApi mockApi = new MockRedditApi();

 
private RedditDb mockDb = RedditDb.Companion.create(
   
ApplicationProvider.getApplicationContext(),
   
true
 
);

 
static {
   
for (int i=0; i<3; i++) {
     
RedditPost post = postFactory.createRedditPost(DEFAULT_SUBREDDIT);
      mockPosts
.add(post);
   
}
 
}

 
@After
 
public void tearDown() {
    mockDb
.clearAllTables();
   
// Clear the failure message after each test run.
    mockApi
.setFailureMsg(null);
   
// Clear out posts after each test run.
    mockApi
.clearPosts();
 
}
}

次に、load() 関数をテストします。この例では、次の 3 つのケースをテストします。

  • 第 1 のケースは、mockApi が有効なデータを返す場合です。load() 関数は MediatorResult.Success を返し、endOfPaginationReached プロパティは false である必要があります。
  • 第 2 のケースは、mockApi が正常なレスポンスを返しても、返されたデータが空である場合です。load() 関数は MediatorResult.Success を返し、endOfPaginationReached プロパティは true である必要があります。
  • 第 3 のケースは、データの取得時に mockApi が例外をスローする場合です。load() 関数は MediatorResult.Error を返す必要があります。

第 1 のケースをテストする手順は次のとおりです。

  1. 返すポストデータで mockApi を設定します。
  2. RemoteMediator オブジェクトを初期化します。
  3. load() 関数をテストします。
KotlinJavaJava
@Test
fun refreshLoadReturnsSuccessResultWhenMoreDataIsPresent() = runTest {
 
// Add mock results for the API to return.
  mockPosts
.forEach { post -> mockApi.addPost(post) }
 
val remoteMediator = PageKeyedRemoteMediator(
    mockDb
,
    mockApi
,
   
SubRedditViewModel.DEFAULT_SUBREDDIT
 
)
 
val pagingState = PagingState<Int, RedditPost>(
    listOf
(),
   
null,
   
PagingConfig(10),
   
10
 
)
 
val result = remoteMediator.load(LoadType.REFRESH, pagingState)
  assertTrue
{ result is MediatorResult.Success }
  assertFalse
{ (result as MediatorResult.Success).endOfPaginationReached }
}
@Test
public void refreshLoadReturnsSuccessResultWhenMoreDataIsPresent()
 
throws InterruptedException {

 
// Add mock results for the API to return.
 
for (RedditPost post: mockPosts) {
    mockApi
.addPost(post);
 
}

 
PageKeyedRemoteMediator remoteMediator = new PageKeyedRemoteMediator(
    mockDb
,
    mockApi
,
   
SubRedditViewModel.DEFAULT_SUBREDDIT
 
);
 
PagingState<Integer, RedditPost> pagingState = new PagingState<>(
   
new ArrayList(),
   
null,
   
new PagingConfig(10),
   
10
 
);
  remoteMediator
.loadSingle(LoadType.REFRESH, pagingState)
   
.test()
   
.await()
   
.assertValueCount(1)
   
.assertValue(value -> value instanceof RemoteMediator.MediatorResult.Success &&
     
((RemoteMediator.MediatorResult.Success) value).endOfPaginationReached() == false);
}
@Test
public void refreshLoadReturnsSuccessResultWhenMoreDataIsPresent()
 
throws InterruptedException, ExecutionException {

 
// Add mock results for the API to return.
 
for (RedditPost post: mockPosts) {
    mockApi
.addPost(post);
 
}

 
PageKeyedRemoteMediator remoteMediator = new PageKeyedRemoteMediator(
    mockDb
,
    mockApi
,
   
SubRedditViewModel.DEFAULT_SUBREDDIT,
   
new CurrentThreadExecutor()
 
);
 
PagingState<Integer, RedditPost> pagingState = new PagingState<>(
   
new ArrayList(),
   
null,
   
new PagingConfig(10),
   
10
 
);

 
RemoteMediator.MediatorResult result =
    remoteMediator
.loadFuture(LoadType.REFRESH, pagingState).get();

  assertThat
(result, instanceOf(RemoteMediator.MediatorResult.Success.class));
  assertFalse
(((RemoteMediator.MediatorResult.Success) result).endOfPaginationReached());
}

第 2 のテストでは、mockApi が空の結果を返す必要があります。各テストの実行後に mockApi からデータを消去するため、デフォルトでは空の結果が返されます。

KotlinJavaJava
@Test
fun refreshLoadSuccessAndEndOfPaginationWhenNoMoreData() = runTest {
 
// To test endOfPaginationReached, don't set up the mockApi to return post
 
// data here.
 
val remoteMediator = PageKeyedRemoteMediator(
    mockDb
,
    mockApi
,
   
SubRedditViewModel.DEFAULT_SUBREDDIT
 
)
 
val pagingState = PagingState<Int, RedditPost>(
    listOf
(),
   
null,
   
PagingConfig(10),
   
10
 
)
 
val result = remoteMediator.load(LoadType.REFRESH, pagingState)
  assertTrue
{ result is MediatorResult.Success }
  assertTrue
{ (result as MediatorResult.Success).endOfPaginationReached }
}
@Test
public void refreshLoadSuccessAndEndOfPaginationWhenNoMoreData()
 
throws InterruptedException() {

 
// To test endOfPaginationReached, don't set up the mockApi to return post
 
// data here.
 
PageKeyedRemoteMediator remoteMediator = new PageKeyedRemoteMediator(
    mockDb
,
    mockApi
,
   
SubRedditViewModel.DEFAULT_SUBREDDIT
 
);
 
PagingState<Integer, RedditPost> pagingState = new PagingState<>(
   
new ArrayList(),
   
null,
   
new PagingConfig(10),
   
10
 
);
  remoteMediator
.loadSingle(LoadType.REFRESH, pagingState)
   
.test()
   
.await()
   
.assertValueCount(1)
   
.assertValue(value -> value instanceof RemoteMediator.MediatorResult.Success &&
     
((RemoteMediator.MediatorResult.Success) value).endOfPaginationReached() == true);
}
@Test
public void refreshLoadSuccessAndEndOfPaginationWhenNoMoreData()
 
throws InterruptedException, ExecutionException {

 
// To test endOfPaginationReached, don't set up the mockApi to return post
 
// data here.
 
PageKeyedRemoteMediator remoteMediator = new PageKeyedRemoteMediator(
    mockDb
,
    mockApi
,
   
SubRedditViewModel.DEFAULT_SUBREDDIT,
   
new CurrentThreadExecutor()
 
);
 
PagingState<Integer, RedditPost> pagingState = new PagingState<>(
   
new ArrayList(),
   
null,
   
new PagingConfig(10),
   
10
 
);

 
RemoteMediator.MediatorResult result =
    remoteMediator
.loadFuture(LoadType.REFRESH, pagingState).get();

  assertThat
(result, instanceOf(RemoteMediator.MediatorResult.Success.class));
  assertTrue
(((RemoteMediator.MediatorResult.Success) result).endOfPaginationReached());
}

最後のテストでは、load() 関数が MediatorResult.Error を正しく返すことをテストで確認できるように、mockApi が例外をスローする必要があります。

KotlinJavaJava
@Test
fun refreshLoadReturnsErrorResultWhenErrorOccurs() = runTest {
 
// Set up failure message to throw exception from the mock API.
  mockApi
.failureMsg = "Throw test failure"
 
val remoteMediator = PageKeyedRemoteMediator(
    mockDb
,
    mockApi
,
   
SubRedditViewModel.DEFAULT_SUBREDDIT
 
)
 
val pagingState = PagingState<Int, RedditPost>(
    listOf
(),
   
null,
   
PagingConfig(10),
   
10
 
)
 
val result = remoteMediator.load(LoadType.REFRESH, pagingState)
  assertTrue
{result is MediatorResult.Error }
}
@Test
public void refreshLoadReturnsErrorResultWhenErrorOccurs()
 
throws InterruptedException {

 
// Set up failure message to throw exception from the mock API.
  mockApi
.setFailureMsg("Throw test failure");
 
PageKeyedRemoteMediator remoteMediator = new PageKeyedRemoteMediator(
    mockDb
,
    mockApi
,
   
SubRedditViewModel.DEFAULT_SUBREDDIT
 
);
 
PagingState<Integer, RedditPost> pagingState = new PagingState<>(
   
new ArrayList(),
   
null,
   
new PagingConfig(10),
   
10
 
);
  remoteMediator
.loadSingle(LoadType.REFRESH, pagingState)
   
.test()
   
.await()
   
.assertValueCount(1)
   
.assertValue(value -> value instanceof RemoteMediator.MediatorResult.Error);
}
@Test
public void refreshLoadReturnsErrorResultWhenErrorOccurs()
 
throws InterruptedException, ExecutionException {

 
// Set up failure message to throw exception from the mock API.
  mockApi
.setFailureMsg("Throw test failure");
 
PageKeyedRemoteMediator remoteMediator = new PageKeyedRemoteMediator(
    mockDb
,
    mockApi
,
   
SubRedditViewModel.DEFAULT_SUBREDDIT,
   
new CurrentThreadExecutor()
 
);
 
PagingState<Integer, RedditPost> pagingState = new PagingState<>(
   
new ArrayList(),
   
null,
   
new PagingConfig(10),
   
10
 
);
 
RemoteMediator.MediatorResult result =
    remoteMediator
.loadFuture(LoadType.REFRESH, pagingState).get();

  assertThat
(result, instanceOf(RemoteMediator.MediatorResult.Error.class));
}

エンドツーエンド テスト

単体テストでは、個々の Paging コンポーネントが独立して動作することが保証されますが、エンドツーエンド テストでは、アプリ全体が動作する確実性が増します。これらのテストはまだモックの依存関係を必要としますが、通常はアプリコードの大部分をカバーします。

このセクションの例では、テストでネットワークを使用しないように、モック API の依存関係を使用します。モック API は、一貫性のあるテストデータのセットを返すように設定されているため、繰り返し可能なテストになります。各依存関係が何を行うか、出力がどの程度一貫しているか、テストにどの程度の忠実度が必要かに基づいて、モック実装に切り替える依存関係を決定します。

依存関係のモック バージョンに簡単に切り替えられるようにコードを記述します。次の例では、基本的なサービス ロケータ実装を使用して、必要に応じて依存関係を指定し、変更します。大規模なアプリでは、Hilt のような依存関係挿入ライブラリを使用することで、より複雑な依存関係グラフを管理できます。

KotlinJavaJava
class RedditActivityTest {

 
companion object {
   
private const val TEST_SUBREDDIT = "test"
 
}

 
private val postFactory = PostFactory()
 
private val mockApi = MockRedditApi().apply {
    addPost
(postFactory.createRedditPost(DEFAULT_SUBREDDIT))
    addPost
(postFactory.createRedditPost(TEST_SUBREDDIT))
    addPost
(postFactory.createRedditPost(TEST_SUBREDDIT))
 
}

 
@Before
 
fun init() {
   
val app = ApplicationProvider.getApplicationContext<Application>()
   
// Use a controlled service locator with a mock API.
   
ServiceLocator.swap(
     
object : DefaultServiceLocator(app = app, useInMemoryDb = true) {
       
override fun getRedditApi(): RedditApi = mockApi
     
}
   
)
 
}
}
public class RedditActivityTest {

 
public static final String TEST_SUBREDDIT = "test";

 
private static PostFactory postFactory = new PostFactory();
 
private static MockRedditApi mockApi = new MockRedditApi();

 
static {
    mockApi
.addPost(postFactory.createRedditPost(DEFAULT_SUBREDDIT));
    mockApi
.addPost(postFactory.createRedditPost(TEST_SUBREDDIT));
    mockApi
.addPost(postFactory.createRedditPost(TEST_SUBREDDIT));
 
}

 
@Before
 
public void setup() {
   
Application app = ApplicationProvider.getApplicationContext();
   
// Use a controlled service locator with a mock API.
   
ServiceLocator.Companion.swap(
     
new DefaultServiceLocator(app, true) {
       
@NotNull
       
@Override
       
public RedditApi getRedditApi() {
         
return mockApi;
       
}
     
}
   
);
 
}
}
public class RedditActivityTest {

 
public static final String TEST_SUBREDDIT = "test";

 
private static PostFactory postFactory = new PostFactory();
 
private static MockRedditApi mockApi = new MockRedditApi();

 
static {
    mockApi
.addPost(postFactory.createRedditPost(DEFAULT_SUBREDDIT));
    mockApi
.addPost(postFactory.createRedditPost(TEST_SUBREDDIT));
    mockApi
.addPost(postFactory.createRedditPost(TEST_SUBREDDIT));
 
}

 
@Before
 
public void setup() {
   
Application app = ApplicationProvider.getApplicationContext();
   
// Use a controlled service locator with a mock API.
   
ServiceLocator.Companion.swap(
     
new DefaultServiceLocator(app, true) {
       
@NotNull
       
@Override
       
public RedditApi getRedditApi() {
         
return mockApi;
       
}
     
}
   
);
 
}
}

テスト構造を設定したら、次に、Pager 実装によって返されたデータが正しいことを確認します。一方のテストでは、ページが最初に読み込まれるときに Pager オブジェクトがデフォルト データを読み込むことを確認し、他方のテストでは、Pager オブジェクトがユーザー入力に基づいて追加のデータを正しく読み込むことを確認します。次の例では、ユーザーが別の subreddit を入力して検索したときに、Pager オブジェクトが API から返された正しい数のアイテムで RecyclerView.Adapter を更新することをテストで確認します。

KotlinJavaJava
@Test
fun loadsTheDefaultResults() {
   
ActivityScenario.launch(RedditActivity::class.java)

    onView
(withId(R.id.list)).check { view, noViewFoundException ->
       
if (noViewFoundException != null) {
           
throw noViewFoundException
       
}

       
val recyclerView = view as RecyclerView
        assertEquals
(1, recyclerView.adapter?.itemCount)
   
}
}

@Test
// Verify that the default data is swapped out when the user searches for a
// different subreddit.
fun loadsTheTestResultsWhenSearchingForSubreddit() {
 
ActivityScenario.launch(RedditActivity::class.java )

  onView
(withId(R.id.list)).check { view, noViewFoundException ->
   
if (noViewFoundException != null) {
     
throw noViewFoundException
   
}

   
val recyclerView = view as RecyclerView
   
// Verify that it loads the default data first.
    assertEquals
(1, recyclerView.adapter?.itemCount)
 
}

 
// Search for test subreddit instead of default to trigger new data load.
  onView
(withId(R.id.input)).perform(
    replaceText
(TEST_SUBREDDIT),
    pressKey
(KeyEvent.KEYCODE_ENTER)
 
)

  onView
(withId(R.id.list)).check { view, noViewFoundException ->
   
if (noViewFoundException != null) {
     
throw noViewFoundException
   
}

   
val recyclerView = view as RecyclerView
    assertEquals
(2, recyclerView.adapter?.itemCount)
 
}
}
@Test
public void loadsTheDefaultResults() {
 
ActivityScenario.launch(RedditActivity.class);

  onView
(withId(R.id.list)).check((view, noViewFoundException) -> {
   
if (noViewFoundException != null) {
     
throw noViewFoundException;
   
}

   
RecyclerView recyclerView = (RecyclerView) view;
    assertEquals
(1, recyclerView.getAdapter().getItemCount());
 
});
}

@Test
// Verify that the default data is swapped out when the user searches for a
// different subreddit.
public void loadsTheTestResultsWhenSearchingForSubreddit() {
 
ActivityScenario.launch(RedditActivity.class);

  onView
(withId(R.id.list)).check((view, noViewFoundException) -> {
   
if (noViewFoundException != null) {
     
throw noViewFoundException;
   
}

   
RecyclerView recyclerView = (RecyclerView) view;
   
// Verify that it loads the default data first.
    assertEquals
(1, recyclerView.getAdapter().getItemCount());
 
});

 
// Search for test subreddit instead of default to trigger new data load.
  onView
(withId(R.id.input)).perform(
    replaceText
(TEST_SUBREDDIT),
    pressKey
(KeyEvent.KEYCODE_ENTER)
 
);

  onView
(withId(R.id.list)).check((view, noViewFoundException) -> {
   
if (noViewFoundException != null) {
     
throw noViewFoundException;
   
}

   
RecyclerView recyclerView = (RecyclerView) view;
    assertEquals
(2, recyclerView.getAdapter().getItemCount());
 
});
}
@Test
public void loadsTheDefaultResults() {
 
ActivityScenario.launch(RedditActivity.class);

  onView
(withId(R.id.list)).check((view, noViewFoundException) -> {
   
if (noViewFoundException != null) {
     
throw noViewFoundException;
   
}

   
RecyclerView recyclerView = (RecyclerView) view;
    assertEquals
(1, recyclerView.getAdapter().getItemCount());
 
});
}

@Test
// Verify that the default data is swapped out when the user searches for a
// different subreddit.
public void loadsTheTestResultsWhenSearchingForSubreddit() {
 
ActivityScenario.launch(RedditActivity.class);

  onView
(withId(R.id.list)).check((view, noViewFoundException) -> {
   
if (noViewFoundException != null) {
     
throw noViewFoundException;
   
}

   
RecyclerView recyclerView = (RecyclerView) view;
   
// Verify that it loads the default data first.
    assertEquals
(1, recyclerView.getAdapter().getItemCount());
 
});

 
// Search for test subreddit instead of default to trigger new data load.
  onView
(withId(R.id.input)).perform(
    replaceText
(TEST_SUBREDDIT),
    pressKey
(KeyEvent.KEYCODE_ENTER)
 
);

  onView
(withId(R.id.list)).check((view, noViewFoundException) -> {
   
if (noViewFoundException != null) {
     
throw noViewFoundException;
   
}

   
RecyclerView recyclerView = (RecyclerView) view;
    assertEquals
(2, recyclerView.getAdapter().getItemCount());
 
});
}

インストルメント化テストでは、データが UI に正しく表示されることを確認する必要があります。これを行うには、RecyclerView.Adapter に正しい数のアイテムが存在することを確認するか、個々の行ビューを反復処理してデータの形式が正しいことを確認します。

現在、おすすめはありません。

Google アカウントにしてください。