user9608350
user9608350

Reputation:

How to use stub() method with Mockito?

I'm learning about Mockito and I try to use stub() method. I have a simple code but it's not working because I get this error: "The method stub(int) is undefined for the type SpyTest". I want to know what dependency should I add in the pom file to use this stub() method? Thank you in advance!

This is the code:

package com.dgs.mockito;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;

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

import org.junit.jupiter.api.Test;

class SpyTest {
    @Test
    void test() {
        List arrayListMock = mock(ArrayList.class);
        assertEquals(0, arrayListMock.size());
        stub(arrayListMock.size()).toReturn(5);
    }
}

And this is the pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dgs.mockito</groupId>
  <artifactId>mockito-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>      
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>

         <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.1</version>
            <scope>test</scope>
         </dependency>

         <dependency>
           <groupId>org.junit.jupiter</groupId>
           <artifactId>junit-jupiter-engine</artifactId>
           <version>5.3.1</version>
        </dependency>

        <dependency>
           <groupId>org.mockito</groupId>
           <artifactId>mockito-core</artifactId>
           <version>2.21.0</version>
        </dependency>

        <dependency>
           <groupId>org.mockito</groupId>
           <artifactId>mockito-junit-jupiter</artifactId>
           <version>2.21.0</version>
        </dependency>

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.2.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

I'm watching a tutorial from Udemy and and he are using this stub method. And if I try to manually add the import I get this error: "The import org.mockito.Mockito.stub cannot be resolved". I think I need to add another dependency in the pom file.

Upvotes: 0

Views: 8654

Answers (2)

GhostCat
GhostCat

Reputation: 140407

Here:

stub(arrayListMock.size()).toReturn(5);

You are calling a method that expects an int argument. But you didn't neither declare nor import such a method. The compiler has no idea what you are trying to do.

The correct way would be:

when(arrayListMock.size()).thenReturn(5);

( after importing when() from Mockito ).

The important thing to understand: there is no specific difference between "mocking" and "stubbing" with Mockito. You simply use when(mock.someMethod()) to start a mocking specification. There is no stub() method in Mockito!

Then: please note that there is a difference between a spy and a mock in Mockito. A mock is a completely "virtual" object that has no relation whatsoever to the real code. A spy is more of an wrapper that Mockito puts "around" a real object of your class under test. Your test is named SpyTest, but it is not using a spy. If it would be using a spy, things are different.

Of course, the real real answer is: you never ever mock lists. You simply create a real list, and you fill it with the elements you need to be inside. Maybe the objects you put into the list are mocks, but there is zero need to mock lists itself.

To the contrary: that is bad practice. You try to minimize the amount of mocks you are using.

Upvotes: 5

Antoniossss
Antoniossss

Reputation: 32507

Try

@Test
void test() {

    List arrayListMock = mock(ArrayList.class);
    doReturn(5).when(arrayListMock ).size();
    assertEquals(5, arrayListMock.size());

}

Upvotes: -4

Related Questions