Kevin Crain Kevin Crain - 1 year ago 155
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(

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

inside of

ActivityMainBinding binding; // <-- this field returns null inside unit test
protected void onCreate(Bundle 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
classed to be assigned before the test completed. Thanks!

Note: I am using
Android Studio 2.2

Answer Source

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:

Thanks to @EugenMartynov for your contribution towards this issue.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download