Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Tests with ObjectBox

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?

like image 415
Marlon Avatar asked Feb 11 '26 06:02

Marlon


1 Answers

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

Check the example repository.

like image 180
Markus Junginger Avatar answered Feb 15 '26 22:02

Markus Junginger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!