I want to write test for the following method:
public void addItem(Item item) {
items.add(0, item);
DatabaseHelper.getInstance().writeOneItem(item);
}
The class is called ItemManager, and it must manage items that the user can save or remove from the list. It should be kept in sync with the Sqlite database, which stores items in a list.
When DatabaseHelper(ormlite) is not included in the number init(Context context)(which usually starts when the Andoid application starts, but does not execute in my test), the method getInstance()returns null, and the method above will crash.
What am I supposed to do here? I could just call init(Context context)from my test or check that DatabaseManager.getInstance()it is null before calling anything on it. But this is more like a workaround. It seems to me that I should not do any database materials in this method and try to separate the ItemManager from the database as much as possible.
Any ideas on how the perfect solution would look, not in the form of a specific implementation, but from a design point of view?
I am new to unit testing and have difficulty decoupling things with each other.
source
share