-
-
Save Sloy/d59a36e6c51214d0b131 to your computer and use it in GitHub Desktop.
import android.os.Parcel; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.mockito.invocation.InvocationOnMock; | |
import org.mockito.stubbing.Answer; | |
import static org.mockito.Matchers.anyInt; | |
import static org.mockito.Matchers.anyLong; | |
import static org.mockito.Matchers.anyString; | |
import static org.mockito.Mockito.doAnswer; | |
import static org.mockito.Mockito.mock; | |
import static org.mockito.Mockito.when; | |
public class MockParcel { | |
public static Parcel obtain() { | |
return new MockParcel().getMockedParcel(); | |
} | |
Parcel mockedParcel; | |
int position; | |
List<Object> objects; | |
public Parcel getMockedParcel() { | |
return mockedParcel; | |
} | |
public MockParcel() { | |
mockedParcel = mock(Parcel.class); | |
objects = new ArrayList<>(); | |
setupMock(); | |
} | |
private void setupMock() { | |
setupWrites(); | |
setupReads(); | |
setupOthers(); | |
} | |
private void setupWrites() { | |
Answer<Void> writeValueAnswer = new Answer<Void>() { | |
@Override public Void answer(InvocationOnMock invocation) throws Throwable { | |
Object parameter = invocation.getArguments()[0]; | |
objects.add(parameter); | |
return null; | |
} | |
}; | |
doAnswer(writeValueAnswer).when(mockedParcel).writeLong(anyLong()); | |
doAnswer(writeValueAnswer).when(mockedParcel).writeString(anyString()); | |
} | |
private void setupReads() { | |
when(mockedParcel.readLong()).thenAnswer(new Answer<Long>() { | |
@Override public Long answer(InvocationOnMock invocation) throws Throwable { | |
return (Long) objects.get(position++); | |
} | |
}); | |
when(mockedParcel.readString()).thenAnswer(new Answer<String>() { | |
@Override public String answer(InvocationOnMock invocation) throws Throwable { | |
return (String) objects.get(position++); | |
} | |
}); | |
} | |
private void setupOthers() { | |
doAnswer(new Answer<Void>() { | |
@Override public Void answer(InvocationOnMock invocation) throws Throwable { | |
position = ((Integer) invocation.getArguments()[0]); | |
return null; | |
} | |
}).when(mockedParcel).setDataPosition(anyInt()); | |
} | |
} |
@jaredburrows, I was wondering the exact same thing! If you check in the stubbed android.jar it's still final, but if you inspect the Parcel class at runtime (in unit tests) it is no longer final, and therefore mockable:
Modifier.toString(Parcel.class.getModifiers())
Returns only "public"! 😮 So I guess Android do something to remove final modifiers so that you can mock them more easily.
Big thanks to Sloy for sharing.
Tip: if anyone has a lot of "reads" to stub, they can be made parametric to shorten the code a bit. eg:
private void setupReads() {
when(mockedParcel.readString()).thenAnswer(stubReadAnswer());
// ... all your reads here ...
when(mockedParcel.readByte()).thenAnswer(stubReadAnswer());
}
private <T> Answer<T> stubReadAnswer() {
return invocation -> (T) objects.get(position++);
}
this does not work if Parcelable class has nullable String field
I made some edits to this approach (and also converted it to Kotlin) - you can check it out here: https://gist.github.com/milosmns/7f6448a3602595948449d3bfaff9b005
It's a good approach overall, I've been able to test everything I needed using this.
In Mockito 2, you can switch to the nullable
matcher to make this match null strings as well:
doAnswer(answer).when(mParcel).writeString(nullable(String.class));
anyString()
used to also match null, but it doesn't anymore: mockito/mockito#185
Also works for nullable Parcelables.
How is this working?
Parcel
is final right?