Reputation: 330
Android Unit tests (not instrumentation test) are running on host machine, and should use fake android.jar
implementation (with stubs) like documentation says.
If I open some Android class in decompiler for instance Rect.java
I can see that all its constructors and methods are throwing Stub!
exceptions as documented.
public final class Rect implements Parcelable {
@NonNull
public static final Parcelable.Creator<Rect> CREATOR = null;
public int bottom;
public int left;
public int right;
public int top;
public Rect() {
throw new RuntimeException("Stub!");
}
public Rect(int left, int top, int right, int bottom) {
throw new RuntimeException("Stub!");
}
public Rect(@Nullable Rect r) {
throw new RuntimeException("Stub!");
}
public boolean equals(Object o) {
throw new RuntimeException("Stub!");
}
public int hashCode() {
throw new RuntimeException("Stub!");
}
public String toString() {
throw new RuntimeException("Stub!");
}
@NonNull
public String toShortString() {
throw new RuntimeException("Stub!");
}
@NonNull
public String flattenToString() {
throw new RuntimeException("Stub!");
}
//....
}
But if I run tests and call Rect()
constructor there, exception is not really thrown (only accessing its methods throws).
The question is, what is the reason for such behaviour? Is decompiler output wrong or why constructor does not throw as expected?
I suppose that there might be cases when constructors should work (like base classes, or for libraries like mockito using inheritance for mocking).
But this Rect
is final
one and it is still not clear why constructor does not throw.
Update: I have also noticed that if I run test with gradle configuration Stub! exception is not thrown from constructor, but if I run test with Kotest configuration it is thrown as expected.
Upvotes: 0
Views: 66