Tengo una clase entidad Client. Esta solo tiene un atributo name tipo String. La clase implementa la interfaces Parcelable de android.os
Se implementaron todo los método para su correcto funcionamiento.
Luego tenemos un Test unitario con JUnit 4. La cual solo prueba convertir la clase al parcel y viceversa. Se coloco en el gradle app la instrucción unitTests.returnDefaultValues = true con el fin de poder hacer uso del framework completo de android para este test.
El problema esta en el momento que pasa por el metodo WriteToParcel se genera un java.lang.NullPointerException. desconocer la razón. mucho les agradecería me guiaran en la solución del error.
Endidad Client
import android.os.Parcel;
import android.os.Parcelable;
public class Client implements Parcelable {
private String name;
public Client(String name) {
this.name = name;
}
protected Client(Parcel in) {
name = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name); //linea 20
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<Client> CREATOR = new Creator<Client>() {
@Override
public Client createFromParcel(Parcel in) {
return new Client(in);
}
@Override
public Client[] newArray(int size) {
return new Client[size];
}
};
}
Test
import android.os.Parcel;
import org.junit.Test;
import static org.junit.Assert.*;
public class ClientTest {
@Test
public void test_Parcel() {
Client tClient = new Client("prueba");
Parcel parcel = Parcel.obtain();
tClient.writeToParcel(parcel, 0); //Linea 18
parcel.setDataPosition(0);
Client createdFromParcel = Client.CREATOR.createFromParcel(parcel);
assertEquals(tClient, createdFromParcel);
}
}
Gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "ve.com.teeac.teeac2"
minSdkVersion 22
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
testOptions {
unitTests.returnDefaultValues = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
def room_version = "2.1.0-alpha04"
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha4'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha05'
implementation "androidx.room:room-runtime:2.1.0-alpha07"
annotationProcessor "androidx.room:room-compiler:$room_version"
testImplementation 'androidx.test:core:1.1.0'
testImplementation "androidx.room:room-testing:$room_version"
androidTestImplementation 'androidx.test:runner:1.1.1'
}
Error mostrado
java.lang.NullPointerException
at ve.com.teeac.teeac2.entities.Client.writeToParcel(Client.java:20)
at ve.com.teeac.teeac2.entities.ClientTest.test_Parcel(ClientTest.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)