【问题标题】:How to use stub() method with Mockito?如何在 Mockito 中使用 stub() 方法?
【发布时间】:2019-02-21 12:27:20
【问题描述】:

我正在学习 Mockito 并尝试使用 stub() 方法。我有一个简单的代码,但它不起作用,因为我收到此错误:“SpyTest 类型的方法存根(int)未定义”。我想知道我应该在 pom 文件中添加什么依赖项才能使用这个 stub() 方法?提前谢谢!

这是代码:

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);
    }
}

这是 pom 文件:

<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>

我正在观看 Udemy 的教程,他正在使用这种存根方法。如果我尝试手动添加导入,我会收到此错误:“无法解析导入 org.mockito.Mockito.stub”。我想我需要在 pom 文件中添加另一个依赖项。

【问题讨论】:

  • stub 是什么?
  • 澄清请求:您的问题得到充分回答了吗?如果是这样,请考虑accepting 一个答案。这样做有助于未来的读者确定问题是否已解决,并对花时间回答您的人表示感谢。否则请提供反馈,以便人们进一步调整他们的答案。

标签: java junit mockito stub


【解决方案1】:

试试

@Test
void test() {

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

}

【讨论】:

  • 此帖子出现在低质量帖子审核队列中。解释一下这段代码如何回答这个问题会很有帮助。
【解决方案2】:

这里:

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

您正在调用一个需要 int 参数的方法。但是您既没有声明也没有导入这样的方法。编译器不知道您要做什么。

正确的方法是:

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

(从 Mockito 导入 when() 之后)。

要理解的重要一点:Mockito 的“嘲笑”和“存根”之间没有具体的区别。您只需使用when(mock.someMethod()) 来启动模拟规范。 Mockito 中没有 stub() 方法!

那么:请注意,Mockito 中的 spymock 是有区别的。模拟是一个完全“虚拟”的对象,与真实代码没有任何关系。间谍更像是 Mockito 将“围绕”您的测试类的真实对象的包装器。您的测试名为 SpyTest,但它不是使用间谍。如果它使用间谍,情况就不同了。

当然,真正的答案是:你永远不会模拟列表。你只需创建一个真实的列表,然后用你需要的元素填充它。也许您放入列表的对象是模拟对象,但 需要模拟列表本身。

恰恰相反:那是不好的做法。您尝试尽量减少您正在使用的模拟量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-16
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 2012-07-19
    • 2020-03-20
    • 2011-01-17
    相关资源
    最近更新 更多