Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test with robolectric #252

Closed
LucioC opened this issue Aug 8, 2014 · 7 comments
Closed

Unit test with robolectric #252

LucioC opened this issue Aug 8, 2014 · 7 comments

Comments

@LucioC
Copy link

LucioC commented Aug 8, 2014

Hello guys, I tried to search this problem but was not yet able to find how to do this.
I just want to unit test a model. I'm using android studio (with gradle) + robolectric 2.3.

I tried the following:
Configuration config = new Configuration.Builder(Robolectric.application)
.addModelClass(MyModel.class)
.setDatabaseName(null)
.create();
ActiveAndroid.initialize(config);

And when I do a new in my model, it raises an exception:
java.lang.NullPointerException
at com.activeandroid.Model.(Model.java:55)

Model.java 54 to 55 has:
mTableInfo = Cache.getTableInfo(getClass());
idName = mTableInfo.getIdName();

So it seems the cache don't have the table info for that class. I tried to do a Cache.initialize(config) also but I still got the same error.

What should I do to make it work?
Thank you.

@LucioC
Copy link
Author

LucioC commented Aug 13, 2014

I was able to resolve it. Something is starting ActiveAndroid before my test, so I do a ActiveAndroid.dispose() before initializing it.

@LucioC LucioC closed this as completed Aug 13, 2014
@IlyaEremin
Copy link

@LucioC hey, what did you do to make it work?

@hdavidzhu
Copy link

@LucioC Do you have a repo with this example?

@LucioC
Copy link
Author

LucioC commented Jan 25, 2016

@IlyaEremin @hdavidzhu hello guys, this was an old issue I had. My problem was that my application class was calling ActiveAndroid initialization, and for some reason that I don't remember, this was not ok for my tests, so I had to dispose and reinitialize.

I think I was also able to create an application test class used by robolectric instead of the default one from my application, and do the initialization there instead of repeating it for each case.

Because this is an older version I'm not sure if things changed. Do you have the same log error?

@hdavidzhu
Copy link

@LucioC for me, just curious to see how to actually implement ActiveAndroid unit tests. The extends couples the data model directly to ActiveAndroid, so I have to run tests through instrumentation. Not too familiar with the Android testing ecosystem in general, so any working examples would be useful.

@LucioC
Copy link
Author

LucioC commented Jan 25, 2016

@hdavidzhu oh right. The only way to mitigate this coupling is using Robolectric. Their default project samples should help you with the default config, that should work with ActiveAndroid.

@hdavidzhu
Copy link

@LucioC Thanks for the input. I'll check out Roboelectric.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants