Jean-Francois Leveque

Couverture satisfaisante de ListPublisherVersionsView par tests

package org.legrog.web.publisher;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.DisplayName;
import org.legrog.entities.Publisher;
import org.legrog.entities.PublisherVersion;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
/**
* Classe testant ListPublisherVersionsView.
*/
@RunWith(MockitoJUnitRunner.class)
@DisplayName("Listes de versions d'éditeurs")
public class ListPublisherVersionsViewTest {
Logger logger = LoggerFactory.getLogger(getClass());
private ListPublisherVersionsView listPublisherVersionsView;
private List<PublisherVersion> publisherVersions;
private PublisherVersion publisherVersion;
private PublisherVersion publisherVersion1;
private PublisherVersion publisherVersion2;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PublisherService publisherService;
@Before
public void setUp() {
listPublisherVersionsView = new ListPublisherVersionsView();
// Données
publisherVersion = new PublisherVersion();
publisherVersion1 = new PublisherVersion();
publisherVersion2 = new PublisherVersion();
publisherVersion.setPublisherVersionId(0);
publisherVersion1.setPublisherVersionId(1);
publisherVersion2.setPublisherVersionId(2);
Publisher publisher = new Publisher();
Publisher publisher1 = new Publisher();
publisher.setPublisherId(0);
publisher1.setPublisherId(1);
logger.trace("publisher {}", publisher);
logger.trace("publisher1 {}", publisher1);
publisherVersion.setPublisher(publisher);
publisherVersion1.setPublisher(publisher1);
publisherVersion2.setPublisher(publisher);
publisher.setValidatedVersion(publisherVersion);
logger.trace("publisherVersion {}", publisherVersion);
logger.trace("publisherVersion1 {}", publisherVersion1);
publisherVersions = new ArrayList<PublisherVersion>();
publisherVersions.add(publisherVersion);
publisherVersions.add(publisherVersion1);
// Mock
when(publisherService.getAllPublisherVersions()).thenReturn(publisherVersions);
listPublisherVersionsView.setPublisherService(publisherService);
logger.trace("listPublisherVersionsView : {}", listPublisherVersionsView);
logger.trace("listPublisherVersionsView, publisherVersions : {}", publisherVersions);
}
@Test
@DisplayName("Liste complète")
public void testSetViewAll() {
listPublisherVersionsView.setPublisherId(null);
listPublisherVersionsView.setView();
Mockito.verify(publisherService).getAllPublisherVersions();
assertThat(listPublisherVersionsView.isViewAll()).isTrue();
assertThat(listPublisherVersionsView.getPublisherVersions()).isEqualTo(this.publisherVersions);
}
@Test
@DisplayName("Liste pour un éditeur")
public void testSetViewNotAllAlsoTestingFilterOnId() {
listPublisherVersionsView.setPublisherId(new Integer(1));
listPublisherVersionsView.setView();
Mockito.verify(publisherService).getAllPublisherVersions();
assertThat(listPublisherVersionsView.isViewAll()).isFalse();
assertThat(listPublisherVersionsView.getPublisherVersions()).containsExactly(this.publisherVersion1);
}
@Test
@DisplayName("Test de l'appel de validation")
public void testValidate() {
listPublisherVersionsView.setPublisherId(new Integer(0));
listPublisherVersionsView.validate(publisherVersion2);
Mockito.verify(publisherService).validatePublisherVersion(publisherVersion2);
}
}
\ No newline at end of file