How to Perform a Single Lead Test in MVP

I have a model view triad presenter. I would like to know how is the usual way to test a presenter.

The first thing that came to my mind was to instantiate the presenter and create a real view, and then assert that the view will do the expected behavior.

public void itShouldSayHello() {
    View view = new View();
    Presenter presenter = new Presenter(view);
    presenter.userSaid("hello");
    assertTrue(view.getGreeting().equals("hello"));
}

      

Then I thought the view was not validated and so I created a fake view.

private String greeting;

public void itShouldSayHello() {
    View view = new FakeView();
    Presenter presenter = new Presenter(view);
    presenter.userSaid("hello");
    assertTrue(greeting.equals("hello"));
}
private class FakeView implements View {
    @Override
    public void displayGreeting(String saluto) {
        greeting = saluto;
    }
}

      

Then I thought that the view interface might change. This would make the code harder to maintain. So I wrote a test and argued that something had to be presented in the view. This way, even if the interface changed, I would have to change one line of code in the tests.

public void itShouldSayHello() {
    View view = mock(View.class);
    Presenter presenter = new Presenter(view);
    presenter.userSaid("hello");
    verify(view).displayGreeting("hello");
}

      

So basically what I am testing now, I expect the presenter to collect and process some information and finally feed it to the view, then I will validate the passed values.

So my guess is that now I am not using fake, I am using mock and then I check if the mocks match the correct values.

Another problem I ran into was the model. But I think it is irresistible. What I have to do to make sure the presenter behaves correctly is to create a big fat tool. Then skip over all the different combinations and see if the leader is leading.

How do you test your host?

+3


source to share


1 answer


You are using the view / presenter separator to test the presenter. If you've fully implemented MVP, your View will implement an interface, and your Presenter will use that interface to return data to the View. If you are testing an MVP application, you want not only your Presenter to function correctly, but also the interface the View uses to communicate with the Presenter.

Thus, your test class must implement the View interface, call Presenter methods, and store responses from the Presenter by local methods that override the interface. If your presenter interacts with the business slot synchronously, it's easier:



  • Define a test class that implements the View interface
  • Implement an interface in a test class so that the data prepared by the presenter is stored in the test object
  • Create test methods in the test class that invoke the presentation, receive a response from the object, and perform appropriate comparisons.

If your Presenter communicates asynchronously, you will have to do something like wait in the test method and notify it about the interface methods.

+2


source







All Articles