Rea*_*hed 5 java android unit-testing robolectric android-toast
我有一个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.
}
}
Run Code Online (Sandbox Code Playgroud)
我想使用 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"));
}
}
Run Code Online (Sandbox Code Playgroud)
看起来他们都没有工作。有任何想法吗?
提前致谢!
我可以设法解决我的问题,并最终得到一个自定义布局来显示 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());
}
}
Run Code Online (Sandbox Code Playgroud)
自定义 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>
Run Code Online (Sandbox Code Playgroud)
显示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();
}
Run Code Online (Sandbox Code Playgroud)
我已经使用了数据绑定,但是,这也应该适用于其他布局膨胀方法。
我希望这可以帮助其他遇到同样问题的开发人员。