首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Java接口中模拟方法时遇到Mockito问题

在Java接口中模拟方法时遇到Mockito问题
EN

Stack Overflow用户
提问于 2022-05-18 16:43:32
回答 1查看 177关注 0票数 0

在使用Junit5和Mockito3.8模拟Java接口中的方法时,我遇到了一些问题。考试失败的原因是:

代码语言:javascript
运行
复制
Wanted but not invoked:
remoteCommands.getServiceStatus("ABC");
-> at com.acme.CommandHandlerTest.testServiceName(CommandHandlerTest.java:40)
Actually, there were zero interactions with this mock.

它在CommandHandlerTest.java的第40行失败了

代码语言:javascript
运行
复制
verify(remoteCommands).getServiceStatus(serviceName);

但是,当我调试代码时,我可以看到正在执行的方法,因此我知道与该方法有1次交互,但是Mockito无法识别它。

如果我遗漏了什么,你能告诉我吗?我的代码文件如下,

CommandHandlerTest.java

代码语言:javascript
运行
复制
package com.acme;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.rmi.RemoteException;

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

@ExtendWith(MockitoExtension.class)
public class  CommandHandlerTest {

    private CommandHandler commandHandler;
    
    @Mock
    IRemoteCommandsImpl remoteCommands;
    
    @BeforeEach
    void setup() {
        MockitoAnnotations.openMocks(this);
    }

    @Test
    void testDoCommand_serviceName() throws RemoteException {
        commandHandler = new CommandHandler();
        
        String serviceName = "ABC";
        int mockValue = 4000;

        when(remoteCommands.getServiceStatus(eq(serviceName))).thenReturn(mockValue);
        
        commandHandler.printServiceStatus(serviceName);
        
        verify(remoteCommands).getServiceStatus(serviceName);
        
    }
}

CommandHandler.java

代码语言:javascript
运行
复制
package com.acme;

public class CommandHandler {

    private IRemoteCommands remoteCommands;
    
    public CommandHandler(){

    }

    public void printServiceStatus(String service) {

        remoteCommands = new IRemoteCommandsImpl();
        int serviceStatus = 0;

        try {
            serviceStatus = remoteCommands.getServiceStatus(service);
            System.out.println("CommandHandler->serviceStatus: "+serviceStatus);
        } catch (Exception e) {
            e.getMessage();
        }
    }
}

IRemoteCommands.java

代码语言:javascript
运行
复制
package com.acme;
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface IRemoteCommands extends Remote
{

    public int getServiceStatus(String serviceName) throws RemoteException;

}

IRemoteCommandsImpl.java

代码语言:javascript
运行
复制
package com.acme;
import java.rmi.RemoteException;

public class IRemoteCommandsImpl implements IRemoteCommands{

    @Override
    public int getServiceStatus(String serviceName) throws RemoteException {
        return 1000;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-19 07:38:33

CommandHandler中,创建一个新的IRemoteCommandsImpl实例并将其分配给私有remoteCommands字段。

这意味着被模拟的remoteCommands不会被被测试的对象使用。

您需要重构代码并将remoteCommands提供给CommandHandler,最好是通过构造函数:

代码语言:javascript
运行
复制
public class CommandHandler {

    private final IRemoteCommands remoteCommands;
    
    public CommandHandler(IRemoteCommands remoteCommands) {
        this.remoteCommands = remoteCommands;
    }
    // ...
}

最重要的是:

如果使用MockitoExtension,则不需要MockitoAnnotations.openMocks(this);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72293034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档