3

I've just started using ObjectBox in my Android apps and I have a simple question about it. This project rests on the Clean Architecture where I have a repository class which receives a BoxStore in its constructor via DI (Dagger2). Inside that constructor, as usual, I create a Box instance using boxStore.boxFor().

It's working perfectly but now I need to test this repository class and... I'm facing some problems that I don't know how to solve.

My first try was to mock BoxStore with Mockito and pass this mock to the repository's constructor. It throws a NullPoinerException. Then, I tried to create a local BoxStore, using the code below:

@Before
public void before() {
    File tempFile = null;
    try {
        tempFile = File.createTempFile("object-store-test", "");
    } catch (IOException e) {
        e.printStackTrace();
    }

    tempFile.delete();
    boxStoreDir = tempFile;
    store = MyObjectBox.builder().directory(boxStoreDir).build();
    repository = new EstadoOrgaoLocalRepository(store);
}

However, now I have the error java.lang.UnsatisfiedLinkError: no objectbox in java.library.path. I don't think installing ObjectBox locally in each developer machine would be a good option here.

So, my simple question is: how can I unit test my repository classes that receive a BoxStore instance via constructor injection?

1 Answer 1

3

You can use ObjectBox for unit tests running locally (Windows, Linux, macOS).

Check the example repository.

Sign up to request clarification or add additional context in comments.

4 Comments

No. I'm using Mac. :( I copied the code in my question from the repository you pointed out. So, is there any other option? Is it possible to use Mocks here?
Any solution dor mac users?
This could be a killer feature - Ability to cover DAO classes with some code and actually it's much simpler for developer to understand how to work with ObjectBox without creating android app. I saw a lot custom solutions (e.g. using h2 for unit tests instead of sqlite). Currently trying to convince to migrate to OB. I'm Mac user too and hopefully we can get some workaround.
good news everyone, Now we have support for macOS: objectbox.io/changelog and example here github.com/objectbox/objectbox-examples/blob/master/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.