pardom-zz / ActiveAndroid

Active record style SQLite persistence for Android
http://www.activeandroid.com
4.7k stars 1.03k forks source link

Can't use GSon with AA in unit tests #457

Open bpappin opened 8 years ago

bpappin commented 8 years ago

I'm writing a unit test suit against a separately packaged model of ActiveAndroid models.

Based on the issue https://github.com/pardom/ActiveAndroid/issues/75, I think the reason is that the models are not registered with AA.

How do I handle the case where the models are not being used inside an Android environment?

The exception I'm getting is: `java.lang.RuntimeException: Failed to invoke public conx2share.cyan.model.Person() with no args at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:111)

[...]

Caused by: java.lang.NullPointerException at com.activeandroid.Cache.getTableInfo(Unknown Source) at com.activeandroid.Model.(Unknown Source) at conx2share.cyan.model.Person.(Person.java:38) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:104)`

jlhonora commented 8 years ago

Could you share the code of the empty constructor for Person?

bpappin commented 8 years ago

Its not going to help much :) public Person() { super(); }

bpappin commented 8 years ago

So the initial problem was that the Model class were not registered properly with AA. However when I did that, I ran into a ton of issues with AA trying to initialize outside the Android environment it was designed to run on.

So, I solved that problem by creating a bunch of stub classes for the android packages, and adding the model class explicitly to the ActiveAndroid config.

I can then init the AA config in the unit test, and use the Model based AA classes in my test. Son no longer complains.

Here are my stubs. Of course, this will need to be modified to be used, but this was enough to get AA to stop complaining so I could work with the models. cyan-android-stubs.zip