SearchViewTest.java 3.01 KB
package org.legrog.web.xyz;

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.Account;
import org.legrog.entities.PublisherVersion;
import org.legrog.entities.SearchingException;
import org.legrog.test.MockitoExtension;
import org.legrog.web.account.AccountService;
import org.legrog.web.publisher.PublisherService;
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 SearchView
 */
@RunWith(JUnitPlatform.class)
@ExtendWith(MockitoExtension.class)
@DisplayName("Searches for an indexed publisher")
public class SearchViewTest {

    private SearchView searchView;
    private PublisherService publisherService;
    private AccountService accountService;

    @BeforeEach
    public void setUp(@Mock PublisherService publisherService, @Mock AccountService accountService) {
        this.publisherService = publisherService;
        this.accountService = accountService;
        this.searchView = new SearchView(publisherService, accountService);
    }

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

        @Test
        @DisplayName("when called, should delegate search to PublisherService and AccountService with same string")
        public void searchUsesPublisherService(@Mock PublisherService publisherService, @Mock AccountService accountService) throws SearchingException{
            searchView.setSearchString("1");
            searchView.search();
            Mockito.verify(publisherService).search("1");
            Mockito.verify(accountService).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);
            searchView.setSearchString("2");
            searchView.search();
            assertThat(searchView.getPublisherVersions()).isEqualTo(publisherVersionList);
        }

        @Test
        @DisplayName("when called, should return the answer it gets from AccountService")
        public void searchReturnsDataFromAccountService(@Mock AccountService accountService) throws SearchingException {
            List<Account> publisherVersionList = new ArrayList<>();
            when(accountService.search("3")).thenReturn(publisherVersionList);
            searchView.setSearchString("3");
            searchView.search();
            assertThat(searchView.getPublisherVersions()).isEqualTo(publisherVersionList);
        }

    }
}