E-MKTO-1 : Apprendre mockito

:timer_clock: 30 minutes

Afin d’apprendre mockito, créez un projet avec junit et mockito. Le site web de mockito explique bien comment installer mockito ainsi que comment l’utiliser. Vous pouvez aussi vous référer à cette page pour utiliser mockito avec maven.

Ajouter ces 2 classes dans src/main/java:

public class Dependency {
    public void acceptParameter(String param) {
        throw new RuntimeException("This should never be called!!");
    }

    public String doSomething() {
        throw new RuntimeException("This should never be called!!");
    }
}
public class LearningMockito {
    public void singleCall(Dependency dependency) {
        dependency.doSomething();
    }

    public void twoCalls(Dependency dependency) {
        dependency.doSomething();
        dependency.doSomething();
    }

    public void singleCallWithParameter(Dependency dependency) {
        dependency.acceptParameter("Hello world");
    }

    public void chainingCalls(Dependency dependency) {
        String result = dependency.doSomething();
        dependency.acceptParameter(result);
    }
}

Ajoutez ensuite cette coquille de tests dans src/test/java:

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class LearningMockitoTest {

    private Dependency dependency;
    private LearningMockito classBeingTested;

    @BeforeEach
    public void setUp() {
        classBeingTested = new LearningMockito();
        // dependency = ??
    }

    @Test
    public void verifyingWhenACallHappensOnce() {
        classBeingTested.singleCall(dependency);

        // Then ??
    }

    @Test
    public void verifyingWhenACallHappensTwice() {
        classBeingTested.twoCalls(dependency);

        // Then ??
    }

    @Test
    public void verifyingWhenAMethodIsCalledWithAParameter() {
        classBeingTested.singleCallWithParameter(dependency);

        // Then ??
    }

    @Test
    public void verifyingWhenAMethodIsCalledWithAnyParameter() {
        classBeingTested.singleCallWithParameter(dependency);

        // Then ??
    }

    @Test
    public void settingUpTheMockToReturnAValue() {
        // Given ??

        classBeingTested.chainingCalls(dependency);

        // Then ??
    }
}

Implémentez les différents tests tel que demandé.

Essayez de le faire d’abord sans la solution. Si vous n’y arrivez pas, utilisez d’abord l’aide d’un collègue. Sinon, voici la solution. Si vous devez vous y réferer, n’oubliez pas de revenir sur cet exercice dans 2 jours.

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.willReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

class LearningMockitoTest {

    private Dependency dependency;
    private LearningMockito classBeingTested;

    @BeforeEach
    public void setUp() {
        classBeingTested = new LearningMockito();
        dependency = mock(Dependency.class);
    }

    @Test
    public void verifyingWhenACallHappensOnce() {
        classBeingTested.singleCall(dependency);

        verify(dependency).doSomething();
    }

    @Test
    public void verifyingWhenACallHappensTwice() {
        classBeingTested.twoCalls(dependency);

        verify(dependency, times(2)).doSomething();
    }

    @Test
    public void verifyingWhenAMethodIsCalledWithAParameter() {
        classBeingTested.singleCallWithParameter(dependency);

        verify(dependency).acceptParameter("Hello world");
    }

    @Test
    public void verifyingWhenAMethodIsCalledWithAnyParameter() {
        classBeingTested.singleCallWithParameter(dependency);

        verify(dependency).acceptParameter(anyString());
    }

    @Test
    public void settingUpTheMockToReturnAValue() {
        String result = "Some result";
        willReturn(result).given(dependency).doSomething();

        classBeingTested.chainingCalls(dependency);

        verify(dependency).acceptParameter(result);
    }
}