MichaelRocks/libphonenumber-android

how to write unit tests

p-fischer opened this issue · 1 comments

Hi,
I chose your Android specific implementation over the Java implementation of google. I want to write unit tests now and I'm wondering if it is possible to create an instance of PhoneNumberUtil without using Context.
In the google library I found TestMetaDataTestCase, which provides an instance and can be inherited from: https://github.com/googlei18n/libphonenumber/blob/4b65742a3fb093e1c2a208d1cc1ef8e0d1b126d5/java/libphonenumber/test/com/google/i18n/phonenumbers/TestMetadataTestCase.java

Can you recommend a way to create an instance of PhoneNumberUtil for unit tests?

Hi,
There's no built-in helper classes for testing. You can create an instance of PhoneNumberUtil using its package-private constructor and providing your own implementation of MetadataLoader. But I believe a better approach would be to wrap libphonenumber in an application-level interface and mock it in tests. In other words you should try and avoid working directly with libphonenumber from your code.