android / android/testing-samples

SharedPreferencesHelperTest is asserting a stub works

オープン
#119 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る
主要言語
Java
スター
9.3k
フォーク
3.6k
PR マージ指標
30日以内にマージされた PR はありません

説明

I see a unit test is written for ``SharedPreferencesHelper`` in BasicTesting project as follows:

/*
* Copyright 2015, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.testing.unittesting.BasicSample;

import android.content.SharedPreferences;
import android.test.suitebuilder.annotation.SmallTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Calendar;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.*;


/**
* Unit tests for the {@link SharedPreferencesHelper} that mocks {@link SharedPreferences}.
*/
@SmallTest
@RunWith(MockitoJUnitRunner.class)
public class SharedPreferencesHelperTest {

private static final String TEST_NAME = "Test name";

private static final String TEST_EMAIL = "test@email.com";

private static final Calendar TEST_DATE_OF_BIRTH = Calendar.getInstance();

static {
TEST_DATE_OF_BIRTH.set(1980, 1, 1);
}

private SharedPreferenceEntry mSharedPreferenceEntry;

private SharedPreferencesHelper mMockSharedPreferencesHelper;

private SharedPreferencesHelper mMockBrokenSharedPreferencesHelper;

@Mock
SharedPreferences mMockSharedPreferences;

@Mock
SharedPreferences mMockBrokenSharedPreferences;

@Mock
SharedPreferences.Editor mMockEditor;

@Mock
SharedPreferences.Editor mMockBrokenEditor;

@Before
public void initMocks() {
// Create SharedPreferenceEntry to persist.
mSharedPreferenceEntry = new SharedPreferenceEntry(TEST_NAME, TEST_DATE_OF_BIRTH,
TEST_EMAIL);

// Create a mocked SharedPreferences.
mMockSharedPreferencesHelper = createMockSharedPreference();

// Create a mocked SharedPreferences that fails at saving data.
mMockBrokenSharedPreferencesHelper = createBrokenMockSharedPreference();
}

@Test
public void sharedPreferencesHelper_SaveAndReadPersonalInformation() {
// Save the personal information to SharedPreferences
boolean success = mMockSharedPreferencesHelper.savePersonalInfo(mSharedPreferenceEntry);

assertThat("Checking that SharedPreferenceEntry.save... returns true",
success, is(true));

// Read personal information from SharedPreferences
SharedPreferenceEntry savedSharedPreferenceEntry =
mMockSharedPreferencesHelper.getPersonalInfo();

// Make sure both written and retrieved personal information are equal.
assertThat("Checking that SharedPreferenceEntry.name has been persisted and read correctly",
mSharedPreferenceEntry.getName(),
is(equalTo(savedSharedPreferenceEntry.getName())));
assertThat("Checking that SharedPreferenceEntry.dateOfBirth has been persisted and read "
+ "correctly",
mSharedPreferenceEntry.getDateOfBirth(),
is(equalTo(savedSharedPreferenceEntry.getDateOfBirth())));
assertThat("Checking that SharedPreferenceEntry.email has been persisted and read "
+ "correctly",
mSharedPreferenceEntry.getEmail(),
is(equalTo(savedSharedPreferenceEntry.getEmail())));
}

@Test
public void sharedPreferencesHelper_SavePersonalInformationFailed_ReturnsFalse() {
// Read personal information from a broken SharedPreferencesHelper
boolean success =
mMockBrokenSharedPreferencesHelper.savePersonalInfo(mSharedPreferenceEntry);
assertThat("Makes sure writing to a broken SharedPreferencesHelper returns false", success,
is(false));
}

/**
* Creates a mocked SharedPreferences.
*/
private SharedPreferencesHelper createMockSharedPreference() {
// Mocking reading the SharedPreferences as if mMockSharedPreferences was previously written
// correctly.
when(mMockSharedPreferences.getString(eq(SharedPreferencesHelper.KEY_NAME), anyString()))
.thenReturn(mSharedPreferenceEntry.getName());
when(mMockSharedPreferences.getString(eq(SharedPreferencesHelper.KEY_EMAIL), anyString()))
.thenReturn(mSharedPreferenceEntry.getEmail());
when(mMockSharedPreferences.getLong(eq(SharedPreferencesHelper.KEY_DOB), anyLong()))
.thenReturn(mSharedPreferenceEntry.getDateOfBirth().getTimeInMillis());

// Mocking a successful commit.
when(mMockEditor.commit()).thenReturn(true);

// Return the MockEditor when requesting it.
when(mMockSharedPreferences.edit()).thenReturn(mMockEditor);
return new SharedPreferencesHelper(mMockSharedPreferences);
}

/**
* Creates a mocked SharedPreferences that fails when writing.
*/
private SharedPreferencesHelper createBrokenMockSharedPreference() {
// Mocking a commit that fails.
when(mMockBrokenEditor.commit()).thenReturn(false);

// Return the broken MockEditor when requesting it.
when(mMockBrokenSharedPreferences.edit()).thenReturn(mMockBrokenEditor);
return new SharedPreferencesHelper(mMockBrokenSharedPreferences);
}
}

in line ``assertThat("Checking that SharedPreferenceEntry.save... returns true",
success, is(true));`` in test ``sharedPreferencesHelper_SaveAndReadPersonalInformation`` you are checking to see if ``commit`` returns ``true`` which is nonsense since you have stubbed mock Editor to return true.

Actually I think testing for this class should have two purposes:

First we should verify method ``putString`` and ``putLong`` have been called by editor in ``savePersonalInfo``.

The second one, is to ensure that ``getPersonalInfo`` have correct conversions.

I'm open to discuss my thoughts with you.
Sincerely.

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

SharedPreferencesHelperTest から始め、特に sharedPreferencesHelper_SaveAndReadPersonalInformation と createMockSharedPreference ヘルパーを確認してください。次に SharedPreferencesHelper の savePersonalInfo と getPersonalInfo を確認し、SharedPreferencesHelperTest を実行してください。完了とは、テストで editor の putString 呼び出しと putLong 呼び出しを検証し、commit のスタブ結果だけに依存せずに getPersonalInfo が行う変換を確認できることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
java
領域
testing
issue の種類
リファクタリング
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
48/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。