Last active
April 22, 2016 02:57
-
-
Save ktchernov/cd3520cebf8aa56214b5155a648d38d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.ktchernov.moshiautovaluebug; | |
import com.squareup.moshi.Moshi; | |
import org.junit.Before; | |
import org.junit.Test; | |
import io.github.ktchernov.moshiautovaluebug.TestClassA.TestClassB; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertNull; | |
public class MoshiAutoValueTest { | |
private Moshi moshi; | |
@Before public void setUp() { | |
moshi = new Moshi.Builder() | |
.add(TestClassB.typeAdapterFactory()).build(); | |
} | |
@Test | |
public void parseValid() throws Exception { | |
TestClassA testClassA = | |
moshi.adapter(TestClassA.class).fromJson("{ \"data\": { \"someValue\": 1 }"); | |
assertEquals(1, testClassA.data.someValue()); | |
} | |
@Test | |
public void parseWithNull() throws Exception { | |
TestClassA testClassA = | |
moshi.adapter(TestClassA.class).fromJson("{ \"data\": null }"); | |
assertNull(testClassA.data); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.ktchernov.moshiautovaluebug; | |
import com.google.auto.value.AutoValue; | |
import android.support.annotation.Nullable; | |
import com.squareup.moshi.JsonAdapter; | |
public class TestClassA { | |
@Nullable public TestClassB data; | |
@AutoValue public static abstract class TestClassB { | |
public abstract int someValue(); | |
public static JsonAdapter.Factory typeAdapterFactory() { | |
return AutoValue_TestClassA_TestClassB.typeAdapterFactory(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment