Mee Noi
Mee Noi

Reputation: 109

How to test a void method within a Presenter using Mockito?

I have a very simple app built using MVP pattern. This is my Contract:

public interface CitiesContract {
    interface View {
        void addCitiesToList(List<City> cityList);
    }

    interface Presenter {
        void getCityList();
    }

    interface Model {
        List<City> getCityList();
    }
}

This is my View:

public class CitiesActivity extends AppCompatActivity implements CitiesContract.View {
    private List<City> cityList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cities);

        CitiesPresenter presenter = new CitiesPresenter(this);
        presenter.getCityList();
    }

    @Override
    public void addCitiesToList(List<City> cities) {
        cityList.addAll(cities);
    }
}

This is my Presenter:

public class CitiesPresenter implements CitiesContract.Presenter {
    private CitiesContract.View view;
    private CitiesModel model;

    public CitiesPresenter(CitiesContract.View view) {
        this.view = view;
        model = new CitiesModel();
    }

    @Override
    public void getCityList() {
        List<City> cityList = model.getCityList();
        view.addCitiesToList(cityList);
    }
}

This is my Model:

public class CitiesModel implements CitiesContract.Model {
    @Override
    public List<City> getCityList() {
        List<City> cityList = new ArrayList<>();
        //Add 30 cities to the list
        return cityList;
    }
}

How can I test the getCityList() method within my Presenter? This is what I have tried so far:

public class CitiesPresenterTest {
    private CitiesContract.Presenter citiesPresenter;
    @Mock
    private CitiesContract.View citiesView;

    public void setUp() {
        MockitoAnnotations.initMocks(this);
        citiesPresenter = new CitiesPresenter(citiesView);
    }

    @Test
    public void getCityListTest() {
        citiesPresenter.getCityList();
        //What to do next???
    }
}

Upvotes: 1

Views: 144

Answers (1)

apksherlock
apksherlock

Reputation: 8371

Ok, that's a good question btw. First of all you also need to mock you model. Second: arrange something: for example that model.getCityList() return null. After that you can verify using mockitos verify operator. Example:

when(model.getCityList()).thenReturn(null);
citiesPresenter.getCityList();
verify(view).addCitiesToList(null);

Another case can be just like that, but with an empty list:

List<City> citiesList = new ArrayList<>();

when(model.getCityList()).thenReturn(citiesList );
citiesPresenter.getCityList();
verify(view).addCitiesToList(citiesList);

And another one can be just like that, with a fake List you can build on your own just to test it:

List<City> citiesList = new ArrayList<>();
list.add(City("name", "something else", "i don't know what atributes you have"));

when(model.getCityList()).thenReturn(citiesList );
citiesPresenter.getCityList();
verify(view).addCitiesToList(citiesList);

Hope I helped.

Additional information: When unit testing you should have 3 basic steps in your head: First you Arrange: So you create your own scenario.Example what if list is null. Second: you Act: this step is where you test the method you want. Third: Assert: this is where you verify or assert that your expectations match with given code.

Upvotes: 1

Related Questions