![](/img/trans.png)
[英]How to unit test the GPS location in android using Robolectric?
[英]Unit test for Toast message content using Robolectric
我有一個Activity
,它只顯示如下 Toast 消息。
public MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Toast.makeText(this, "Some message", Toast.LENGTH_LONG).show();
finish(); // Finish the activity here.
}
}
我想使用 Robolectric 編寫一個單元測試來驗證Toast
內容。 我嘗試了以下方法。
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
@Before
public void setup() {
Robolectric.buildActivity(MyActivity.class).create();
}
@Test
public void testToast() {
assertTrue(ShadowToast.showedCustomToast("Some message", R.string.some_message));
assertThat(ShadowToast.getTextOfLatestToast().toString(), equalTo("Some message"));
}
}
看起來他們都沒有工作。 有任何想法嗎?
提前致謝!
我可以設法解決我的問題,並最終獲得了一個自定義布局,用於顯示 Toast 消息並使用 Robolectric 測試 Toast 的內容,如下所示。 我在這里添加了一個帶有工作示例的GitHub 項目。
public class MainActivityTest {
@Before
public void setup() {
Robolectric.buildActivity(MainActivity.class).create();
}
@Test
public void testToastMessage() {
ToastLayoutBinding binding = DataBindingUtil.getBinding(ShadowToast.getLatestToast().getView());
binding.executePendingBindings();
assertEquals(binding.toastMsg.getContext().getString(R.string.some_toast),
binding.toastMsg.getText());
}
@Test
public void testToastDuration() {
assertEquals(Toast.LENGTH_LONG, ShadowToast.getLatestToast().getDuration());
}
}
自定義 toast 的布局如下。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.text.TextUtils" />
<variable
name="msg"
type="String" />
</data>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="4dp">
<TextView
android:id="@+id/toast_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:gravity="center"
android:text="@{TextUtils.isEmpty(msg) ? @string/no_msg : msg}" />
</FrameLayout>
</layout>
並且顯示toast消息的函數如下。
/**
* Display a custom toast with some message provided as a function parameter
*
* @param activity An {@link Activity} where the toast message will be shown
* @param msg a {@link String} that holds the message to be shown as a Toast
* @throws IllegalArgumentException if a null activity is passed to the function
* @apiNote If a null String is passed as a #msg parameter, then this function shows a default text (no_toast)
*/
public static void showToast(Activity activity, String msg) {
if (activity == null) {
throw new IllegalArgumentException("The passed in activity cannot be null");
}
if (msg == null) {
msg = activity.getString(R.string.no_msg);
}
ToastLayoutBinding toastLayout = DataBindingUtil.inflate(
activity.getLayoutInflater(), R.layout.toast_layout, null, false);
toastLayout.setMsg(msg); // Set the toast message here
Toast toast = new Toast(activity);
toast.setView(toastLayout.getRoot());
toast.setGravity(Gravity.TOP, 0, 200);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
我使用過數據綁定,但是,這也應該適用於其他布局膨脹方法。
我希望能幫助其他有同樣問題的開發人員。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.