I am writing unit test using Mockito for a method which returns Account object.
I am creating a new account as following:
Private Account testAccount = new Account("name", "type");
Code is not crashing but I am always getting this exception when I debug:
Method threw 'java.lang.RuntimeException' exception. Cannot evaluate android.accounts.Account.toString()
And testAccount.name
and testAccount.type
is always null
.
Can someone tell me if I am doing something wrong or if there is a proper way to mock it and get same account name and type as defined at time of initialisation?
A colleague at work figured this out that we have to do reflection for the Account object as its fields are defined final, so you have to do it as following:
Account account = new Account("MyAccount", "SomeType");
Field nameField = account.getClass().getDeclaredField("name");
nameField.setAccessible(true);
nameField.set(account, "your value");
// Set whatever field you want to configure
Field typeField = account.getClass().getDeclaredField("type");
typeField.setAccessible(true);
typeField.set(account, "your value");