PublisherSearchViewTest.java 2.19 KB
package org.legrog.web.publisher;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.legrog.entities.PublisherVersion;
import org.legrog.entities.SearchingException;
import org.legrog.test.MockitoExtension;
import org.mockito.Mock;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

/**
 * Classe testant PublisherSearchView
 */
@RunWith(JUnitPlatform.class)
@ExtendWith(MockitoExtension.class)
@DisplayName("Searches for an indexed publisher")
public class PublisherSearchViewTest {

    private PublisherSearchView publisherSearchView;
    private PublisherService publisherService;

    @BeforeEach
    public void setUp(@Mock PublisherService publisherService) {
        this.publisherService = publisherService;
        this.publisherSearchView = new PublisherSearchView(publisherService);
    }

    @Nested
    @DisplayName("search method")
    class SearchTests {

        @Test
        @DisplayName("when called, should delegate search to PublisherService with same string")
        public void searchUsesPublisherService(@Mock PublisherService publisherService) throws SearchingException{
            publisherSearchView.setSearchString("1");
            publisherSearchView.search();
            Mockito.verify(publisherService).search("1");
        }

        @Test
        @DisplayName("when called, should return the answer it gets from PublisherService")
        public void searchReturnsDataFromPublisherService(@Mock PublisherService publisherService) throws SearchingException {
            List<PublisherVersion> publisherVersionList = new ArrayList<>();
            when(publisherService.search("2")).thenReturn(publisherVersionList);
            publisherSearchView.setSearchString("2");
            publisherSearchView.search();
            assertThat(publisherSearchView.getPublisherVersions()).isEqualTo(publisherVersionList);
        }
    }
}