For example, simple Parcelable
implementation:
public class Student implements Parcelable {
public static final Creator<Student> CREATOR = new Creator<Student>() {
@Override
public Student createFromParcel(Parcel parcel) {
return new Student(parcel);
}
@Override
public Student[] newArray(int size) {
return new Student[size];
}
};
private final Integer academicYear;
private final Integer age;
private final String name;
public Student() {
this.name = null;
this.age = null;
this.academicYear = null;
}
public Student(String name, int age, int academicYear) {
this.name = name;
this.age = age;
this.academicYear = academicYear;
}
public Student(Student origin) {
this.name = origin.getName();
this.age = origin.getAge();
this.academicYear = origin.getAcademicYear();
}
public Student(Parcel in) {
this.name = in.readString();
this.age = (Integer) in.readValue(Integer.class.getClassLoader());
this.academicYear = (Integer) in.readValue(Integer.class.getClassLoader());
}
@Override
public void writeToParcel(Parcel out, int i) {
out.writeString(name);
out.writeValue(age);
out.writeValue(academicYear);
}
@Override
public int describeContents() {
return 0;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public Integer getAcademicYear() {
return academicYear;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Student)) {
return false;
}
Student other = (Student) obj;
return (Objects.equal(getName(), other.getName()) &&
Objects.equal(getAge(), other.getAge()) &&
Objects.equal(getAcademicYear(), other.getAcademicYear()));
}
@Override
public int hashCode() {
return Objects.hashCode(getName(), getAge(), getAcademicYear());
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("name", getName())
.add("age", getAge())
.add("academicYear", getAcademicYear())
.toString();
}
}
And this test doesn't pass:
public void testParcelable() {
Student studentIn = new Student("Jack", 19, 2);
Parcel parcel = Parcel.obtain();
studentIn.writeToParcel(parcel, 0);
Student studentOut = Student.CREATOR.createFromParcel(parcel);
assertEquals(studentIn, studentOut);
}
expected: Student{name=Jack, age=25, academicYear=6} but was:Student{name=null, age=null, academicYear=null}
What is wrong? How to get and use Parcel
correctly to test Parcelable
implementation ?
Or should I use Intent
instead of Parcel
? If I use Intent
then test passes.
0 comments:
Post a Comment