Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Robolectric对Toast消息内容进行单元测试_Java_Android_Unit Testing_Robolectric_Android Toast - Fatal编程技术网

Java 使用Robolectric对Toast消息内容进行单元测试

Java 使用Robolectric对Toast消息内容进行单元测试,java,android,unit-testing,robolectric,android-toast,Java,Android,Unit Testing,Robolectric,Android Toast,我有一个活动,它除了显示如下祝酒词外,什么也不做 public MyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); 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. 
    }
}
@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"));
    }
}
我想使用Robolectric编写一个单元测试来验证
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. 
    }
}
@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的内容,如下所示。我在这里添加了一个工作示例

公共类维护活动测试{
@以前
公共作废设置(){
Robolectric.buildActivity(MainActivity.class).create();
}
@试验
public void testToastMessage(){
toastalayoutbinding binding=DataBindingUtil.getBinding(ShadowToast.getLatestToast().getView());
binding.executePendingBindings();
assertEquals(binding.toastMsg.getContext().getString(R.string.some_toast),
binding.toastMsg.getText());
}
@试验
公共void testToastDuration(){
assertEquals(Toast.LENGTH_LONG,ShadowToast.getLatestToast().getDuration());
}
}
定制toast的布局如下所示


显示toast消息的功能如下所示

/**
*显示自定义toast,并将某些消息作为函数参数提供
*
*@param activity将显示toast消息的{@link activity}
*@param msg是一个{@link String},其中包含要显示为Toast的消息
*如果将null活动传递给函数,@将引发IllegalArgumentException
*@apiNote如果将空字符串作为#msg参数传递,则此函数将显示默认文本(no#toast)
*/
公共静态void showtoos(活动活动,字符串消息){
如果(活动==null){
抛出新的IllegalArgumentException(“传入的活动不能为null”);
}
如果(msg==null){
msg=activity.getString(R.string.no_msg);
}
ToastLayoutBinding toastLayout=DataBindingUtil.inflate(
activity.getLayoutFlater(),R.layout.toast_layout,null,false);
toastLayout.setMsg(msg);//在此处设置toast消息
吐司吐司=新吐司(活动);
toast.setView(toastLayout.getRoot());
toast.setGravity(Gravity.TOP,0200);
toast.setDuration(toast.LENGTH\u LONG);
toast.show();
}
我已经使用了数据绑定,但是,这也应该适用于其他布局膨胀方法


我希望这能帮助其他有同样问题的开发人员

这在几个层面上似乎有问题:1)你可以在任何地方敬酒:为什么要创建一个活动来做呢?2) 为什么要测试Android Toast系统?如果它不起作用,你会怎么做?3) Toast是活页夹的薄包装:它将文本传输到一个完全不同的应用程序,该应用程序打印消息。这将是一个相当困难的阴影。你试过阴影土司吗?showedToast(CharSequence message)?@ahasbini是的,我想我也试过了。谢谢你的评论@布莱克梅克非常感谢你的评论。我试图在这里作出回应。该活动只是一个提供示例的虚拟对象。我不想测试Android Toast系统。我想测试一下吐司的内容,看看是否有。看起来Robolectric提供了一些阴影技术,但到目前为止没有一个对我有效。@ReazMurshed你可以称它为
Toast
,但它实际上不再是
Toast
(正如Blake解释的那样)。我们只能想象,使用
UiAutomator2
(可以访问应用程序范围之外的视图)可以获得实际
Toast
的句柄。