我有三种方法.当运行单元测试时,我需要下一个:

  1. 方法serviceMethod_1不调用REAL方法
  2. 方法serviceMethod_2调用REAL方法
  3. 方法serviceMethod_1调用REAL方法

这里定义了方法Mockito.spy()

Mockito.spy() - each method implementation are the real one, except if you have defined a mocked behaviour with when(..)

这正是我所需要的.

因此,下面是我的单元测试:

import org.mockito.Spy;
@Spy
private MyServiceImpl myServiceSpy;

@Before
    public void setup() {
        System.out.println("\nSETUP");
    }

    @Test
    public void myService_method_1_spy() {
        when(myServiceSpy.serviceMethod_1())
                .thenReturn("call_serviceMethod_1_MOCK");

        List<String> actualList = myServiceSpy.performService();
        

        List<String> expectedList = Arrays.asList(
                "call_serviceMethod_1_MOCK",
                "call_serviceMethod_2_REAL",
                "call_serviceMethod_3_REAL");
        assertEquals(expectedList, actualList);
       
        verify(myServiceSpy, times(1)).serviceMethod_1();
    }

这里的服务级别:

public class MyServiceImpl implements MyService {
    private String text;

    @Override
    public String serviceMethod_1() {
        String result = "call_serviceMethod_1_REAL";
        System.out.println("serviceMethod_1: result = " + result);
        return result;
    }

    @Override
    public String serviceMethod_2() {
        String result = "call_serviceMethod_2_REAL";
        System.out.println("serviceMethod_2: result = " + result);
        return result;
    }

    @Override
    public String serviceMethod_3() {
        String result = "call_serviceMethod_3_REAL";
        System.out.println("serviceMethod_3: result = " + result);
        return result;
    }

    @Override
    public List<String> performService() {
        System.out.println("performService: START, this.className = " + this.getClass().getName()); // In the name contain word "MockitoMock"
        List<String> list = new ArrayList<>();
        String item_1 = serviceMethod_1();
        String item_2 = serviceMethod_2();
        String item_3 = serviceMethod_3();
        list.add(item_1);
        list.add(item_2);
        list.add(item_3);
        return list;
    }

    public void setText(String text) {
        this.text = text;
    }
}

但是,当我运行测试时,我会得到下一个输出:

SETUP
serviceMethod_1: result = call_serviceMethod_1_REAL
performService: START, this.className = com.myproejctjava.javatestproject.service.MyServiceImpl$MockitoMock$Il2iukBL
serviceMethod_2: result = call_serviceMethod_2_REAL
serviceMethod_3: result = call_serviceMethod_3_REAL
BUILD SUCCESSFUL in 1s

正如您在测试调用REAL方法serviceMethod_1中看到的那样.

为什么方法spy()对我没有帮助?我不需要调用REAL方法serviceMethod_1

推荐答案

使用

doReturn("call_serviceMethod_1_MOCK").when(myServiceSpy).serviceMethod_1();
    

而不是

when(myServiceSpy.serviceMethod_1()).thenReturn("call_serviceMethod_1_MOCK");

Java相关问答推荐

为什么我们仍然需要实现noArgsConstructor如果Java默认提供一个非参数化的构造函数?''

使用java访问具体子类特定方法的最佳方法是什么?

无法在WebSocket onMessage中捕获错误

R.id.main给我一个红色错误,无法解析MainActivity.java中的符号main

如何使用SpringBoot中的可分页对整数作为字符串存储在数据库中时进行排序

使用Spring和ActiveMQ的侦听器方法引发属性名称不能重复为空警告

如何在Microronaut中将 map 读取为 map

try 在Android Studio中的infoWindow中使用EditText(Java)

Regex以查找不包含捕获组的行

FETCH类型设置为LAZY,但它仍会发送第二个请求

无法将GSON导入到我的JavaFX Maven项目

如何在IntelliJ IDEA的Build.sbt中添加外部JAR文件?

Java 21中泛型的不兼容更改

如何在字节数组中反转UTF-8编码?

如何在Record Java中使用isRecord()和RecordComponent[]?

按长度排序字符串数组

如何使JOOQ渐变脚本不重新创建表未更改的类?

始终使用Spring Boot连接mongodb上的测试数据库

我可以使用一个 PoolingNHttpClientConnectionManager 运行多个 HttpAsyncClient 吗?

Keycloak + Spring Boot 角色认证不起作用