Kevin Crain Kevin Crain - 25 days ago 14
Android Question

Get activity `ViewDataBinding` field inside `Robolectric` unit test after being assigned

I am writing a Robolectric unit test which requires me to make use of the activity under test view data binding class(

ViewDataBinding
), unfortunately with no luck I am stuck with it being null inside of the unit test

inside of
MainActiviy
class:

...
ActivityMainBinding binding; // <-- this field returns null inside unit test
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
}
...


If anyone has written a test with a similar requirement please could you demonstrate an example of how you got the
ViewDataBinding
classed to be assigned before the test completed. Thanks!


Note: I am using
Android Studio 2.2


Answer

I found out that testing with Robolectric version 3.1 was my issue and after upgrading to using Robolectric version 3.1.4 the activities ViewDataBinding field was no longer returning null

See PR: https://github.com/emartynov/android-architecture/pull/1

Thanks to @EugenMartynov for your contribution towards this issue.