FVod FVod - 1 year ago 113
Android Question

Mock model object without setters

I'm implementing tests on my Android app and I want to do unit tests on a model. The problem is that I do not have setters as the model is created with Realm or by parsing a CSV file (witch univocity csv parser).
So, how could I create a mocked object with valid values? I have something like that:

public class Content {

private String title;
private String description;


How could I generate a mocked Content object with a title and description data?

Thanks in advance

Answer Source

Use code below in your test class:

    Field field = Content.class.getDeclaredField("str");
    field.set(yourObject, "some value");

yourObject is a instance of Content that you use in your test class.

But you shouldn't fill mock object - you should just define method result for mock object.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download