Navigation Menu

Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #96 from AlexanderPraegla/hm-edu-testing
Browse files Browse the repository at this point in the history
[JENKINS-50074] Added Mockito Test for ByIdResultSelector
  • Loading branch information
uhafner committed Apr 11, 2018
2 parents 6d9a427 + 2267c09 commit 52a55ed
Showing 1 changed file with 116 additions and 0 deletions.
@@ -0,0 +1,116 @@
package io.jenkins.plugins.analysis.core.model;

import hudson.model.Run;
import io.jenkins.plugins.analysis.core.views.ResultAction;
import org.junit.jupiter.api.Test;

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

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

/**
* Tests the class {@link ByIdResultSelector}
*
* @author Alexander Praegla
*/
class ByIdResultSelectorTest {

/**
* Verifies the returned optional is empty if the provided list of {@link ResultAction} is empty
*/
@Test
void shouldBeEmptyIfActionsAreEmpty() {
ByIdResultSelector cut = new ByIdResultSelector("1");

Run<?, ?> run = createRunStub(new ArrayList<>());

assertThat(cut.get(run)).isEmpty();
}

/**
* Verifies the returned optional is empty if the provided list of {@link ResultAction}
* does not contains the id of the instance of {@link ByIdResultSelector}
*/
@Test
void shouldBeEmptyIfActionsHasDifferentId() {
ByIdResultSelector cut = new ByIdResultSelector("1");

List<ResultAction> resultActions = new ArrayList<>();
resultActions.add(createResultActionStub("2"));

Run<?, ?> run = createRunStub(resultActions);

assertThat(cut.get(run)).isEmpty();
}

/**
* Verifies the returned optional is not empty if the provided list of {@link ResultAction} contains one element
*/
@Test
void shouldFindActionWithSameId() {
ByIdResultSelector cut = new ByIdResultSelector("1");

List<ResultAction> resultActions = new ArrayList<>();
resultActions.add(createResultActionStub("1"));

Run<?, ?> run = createRunStub(resultActions);

assertThat(cut.get(run)).isPresent();
}

/**
* Verifies the returned optional is empty if the provided list of {@link ResultAction} contains two elements with different id
*/
@Test
void shouldFindEmptyIfListContainsTwoDifferentIds() {
ByIdResultSelector cut = new ByIdResultSelector("1");

List<ResultAction> resultActions = new ArrayList<>();
resultActions.add(createResultActionStub("2"));
resultActions.add(createResultActionStub("3"));

Run<?, ?> run = createRunStub(resultActions);

assertThat(cut.get(run)).isEmpty();
}

/**
* Verifies the returned optional is not empty if the provided list of {@link ResultAction} contains two elements
*/
@Test
void shouldFindActionWithSameIdIfListContainsTwoDifferentIds() {
ByIdResultSelector cut = new ByIdResultSelector("1");

List<ResultAction> resultActions = new ArrayList<>();
resultActions.add(createResultActionStub("2"));
resultActions.add(createResultActionStub("1"));

Run<?, ?> run = createRunStub(resultActions);

assertThat(cut.get(run)).isPresent();
}

/**
* Verifies the toString method
*/
@Test
void testToString() {
ByIdResultSelector cut = new ByIdResultSelector("1");
assertThat(cut.toString()).isEqualTo("io.jenkins.plugins.analysis.core.views.ResultAction with ID 1");
}

private Run<?, ?> createRunStub(List<ResultAction> resultActions) {
Run<?, ?> run = mock(Run.class);
when(run.getActions(ResultAction.class)).thenReturn(resultActions);
return run;
}

private ResultAction createResultActionStub(String mockedIdValue) {
ResultAction resultAction = mock(ResultAction.class);
when(resultAction.getId()).thenReturn(mockedIdValue);
return resultAction;
}
}

0 comments on commit 52a55ed

Please sign in to comment.