Mockito 3 Answere RETURNS MOCKS

An unstubbed method in normal scenario it will return null, but what if mockito tries to figure out which type of response is and create a new response, that's exactly what RETURNS_MOCKS does.

in below example appleService.getApple() is not mocked hence by default it will return null and assertEquals(apple, apple1) will be true.

package com.example.mokito3.sujan;

public class AppleService {
    private Apple apple;

    public Apple getApple() {
        return apple;
    }

    public class Apple {
    }
}
package com.example.mokito3.sujan;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.Mockito.mock;

@ExtendWith(MockitoExtension.class)
public class AppleServiceTest {
    @Mock
    private AppleService appleService;

    @Test
    void saveAppleWithStaticMockTest() {
        AppleService appleService = mock(AppleService.class);
        AppleService.Apple apple = appleService.getApple();
        AppleService.Apple apple1 = appleService.getApple();
        System.out.println("apple = " + apple);
        System.out.println("apple1 = " + apple1);
        Assertions.assertEquals(apple, apple1);
    }

    @Test
    void saveAppleWithAnnotationMockTest() {
        AppleService.Apple apple = appleService.getApple();
        AppleService.Apple apple1 = appleService.getApple();
        System.out.println("apple = " + apple);
        System.out.println("apple1 = " + apple1);
        Assertions.assertEquals(apple, apple1);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories { jcenter() }
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
    testCompile 'org.mockito:mockito-junit-jupiter:3.4.4'
}
test {
    useJUnitPlatform()
}

 

with RETURNS_MOCKS, mockito tries to figure out what is the response type of the un-stubbed method and will return a new object. hence assertNotEquals(apple, apple1) will be true.

package com.example.mokito3.sujan;

public class AppleService {
    private Apple apple;

    public Apple getApple() {
        return apple;
    }

    public class Apple {
    }
}
package com.example.mokito3.sujan;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.Answers.RETURNS_MOCKS;
import static org.mockito.Mockito.mock;

@ExtendWith(MockitoExtension.class)
public class AppleServiceTest {
    @Mock(answer = RETURNS_MOCKS)
    private AppleService appleService;

    @Test
    void saveAppleWithStaticMockTest() {
        AppleService appleService = mock(AppleService.class, RETURNS_MOCKS);
        AppleService.Apple apple = appleService.getApple();
        AppleService.Apple apple1 = appleService.getApple();
        System.out.println("apple = " + apple);
        System.out.println("apple1 = " + apple1);
        Assertions.assertNotEquals(apple, apple1);
    }

    @Test
    void saveAppleWithAnnotationMockTest() {
        AppleService.Apple apple = appleService.getApple();
        AppleService.Apple apple1 = appleService.getApple();
        System.out.println("apple = " + apple);
        System.out.println("apple1 = " + apple1);
        Assertions.assertNotEquals(apple, apple1);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories { jcenter() }
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
    testCompile 'org.mockito:mockito-junit-jupiter:3.4.4'
}
test {
    useJUnitPlatform()
}

 

RETURNS_MOCKS not only returns the mock object of the unstubbed method, but it also stubs all the variables inside as well. in this case RETURNS_MOCKS will not only stub Apple class as well as it's dependent Macintosh class as well.

package com.example.mokito3.sujan;

public class AppleService {
    private Apple apple;

    public Apple getApple() {
        return apple;
    }

    public class Apple {
        private Macintosh macintosh;

        public Macintosh getMacintosh() {
            return macintosh;
        }
    }

    public class Macintosh {
    }
}
package com.example.mokito3.sujan;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.Answers.RETURNS_MOCKS;
import static org.mockito.Mockito.mock;

@ExtendWith(MockitoExtension.class)
public class AppleServiceTest {
    @Mock(answer = RETURNS_MOCKS)
    private AppleService appleService;

    @Test
    void saveAppleWithStaticMockTest() {
        AppleService appleService = mock(AppleService.class, RETURNS_MOCKS);
        AppleService.Apple apple = appleService.getApple();
        AppleService.Apple apple1 = appleService.getApple();
        System.out.println("apple = " + apple);
        System.out.println("apple1 = " + apple1);
        Assertions.assertNotEquals(apple, apple1);
        System.out.println("apple.Macintosh = " + apple.getMacintosh());
        System.out.println("apple1.Macintosh = " + apple1.getMacintosh());
        Assertions.assertNotEquals(apple.getMacintosh(), apple1.getMacintosh());
    }

    @Test
    void saveAppleWithAnnotationMockTest() {
        AppleService.Apple apple = appleService.getApple();
        AppleService.Apple apple1 = appleService.getApple();
        System.out.println("apple = " + apple);
        System.out.println("apple1 = " + apple1);
        Assertions.assertNotEquals(apple, apple1);
        System.out.println("apple.Macintosh = " + apple.getMacintosh());
        System.out.println("apple1.Macintosh = " + apple1.getMacintosh());
        Assertions.assertNotEquals(apple.getMacintosh(), apple1.getMacintosh());
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories { jcenter() }
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
    testCompile 'org.mockito:mockito-junit-jupiter:3.4.4'
}
test {
    useJUnitPlatform()
}

 

follow us on