pilgr / Paper

Paper is a fast NoSQL-like storage for Java/Kotlin objects on Android with automatic schema migration support.
Apache License 2.0
2.35k stars 234 forks source link

Paper.init is not called - Unit Testing #140

Closed Akramhussain4 closed 5 years ago

Akramhussain4 commented 5 years ago

io.paperdb.PaperDbException: Paper.init is not called when I try to run code for unit testing using mockito. I am unable to initialize paper in UnitTesting class.

pilgr commented 5 years ago

If you run unit tests on local jvm you don't have access to the Context so you can't use Paper directly and must mock its methods. If you run instrumentation tests you should call Paper.init() prior running any tests )in your setUp method).

Akramhussain4 commented 5 years ago

I am running unit tests in Android Studio and I can't pass the context of a unit test class. I tried calling Paper.init() with the context of unit class in setUp method but it just doesn't take that context or the mocked context.

J6ey commented 5 years ago

I am running unit tests in Android Studio and I can't pass the context of a unit test class. I tried calling Paper.init() with the context of unit class in setUp method but it just doesn't take that context or the mocked context.

Like he said, for a local jvm unit test you gotta mock the init() method from Paper.class. But since this call is static you can try using PowerMockito to mock it.