Monday 14 December 2020

Android Roboelectric test: null pointer in getting view id in Custom Toast

I have a Custom Toast class -

public static void makeToast(final Context context, String message) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View layout = inflater.inflate(R.layout.toast_with_icon, null, false);

        TextView text = layout.findViewById(R.id.text);
        text.setText(message);
        text.setTextColor(Color.RED);

        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast toast = new Toast(context.getApplicationContext());
                toast.setView(layout);
                toast.show();
            }
        });
    }

I want to get the text in Roboelectric when this Toast class is shown and assert it, for which I'm using this -

var checkMessage = ShadowToast.showedCustomToast("Login failed. your credentials are not valid", R.id.text)
assert(checkMessage)

But that gives me a null pointer for R.id.text-

java.lang.NullPointerException at org.robolectric.shadows.ShadowToast.showedCustomToast(ShadowToast.java:159) at in.novopay.novoloan.ui.login.data.LoginRepositoryTest.testLoginWithWrongCredentials(LoginRepositoryTest.kt:56)

Is my way of accessing the custom Toast layout correct? Can there be a better way of asserting the custom Toast message ?



from Android Roboelectric test: null pointer in getting view id in Custom Toast

No comments:

Post a Comment