【问题标题】:how do i mock method, calling one method from another method?我如何模拟方法,从另一种方法调用一个方法?
【发布时间】:2020-06-03 13:08:09
【问题描述】:

我是 Mocking 的新手。 我有一个我想调用的服务,比如说名字 A,我需要测试一些方法。

@Service
public class A {



private Logger logger = LoggerFactory.getLogger(getClass());

    private final CoreXReader coreXReader;

    @Autowired
    B b;

    @Autowired
    C c;


    @Async
    public void someMethod(Config config) throws Exception {
        pushConfig(config);
    }

    private void pushConfig(Config config) throws Exception {

        String url = config.getBaseurl() + config.getId(); 
        ABCRestClient restClient = new ABCRestClient(url);

            String jobJson = restClient.callRestMethod(HttpMethod.GET, "");

    } 

} 

ABCRestClient 示例

  public class ABCRestClient {
        private Logger logger = LoggerFactory.getLogger(getClass());


        private String url;

        public ABCRestClient(String url) {
            this.url = url;
        }


        public String callRestMethod(HttpMethod method, String payload) throws Exception {
            someresponse="example response";
            return someresponse;
        }
    }

我正在尝试通过创建 mockSpy 进行测试,但它仍然调用它的“callRestMethod”

@RunWith(SpringRunner.class)
@SpringBootTest // (webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Test {

    @Autowired
    private A a;



    private Logger logger = LoggerFactory.getLogger(getClass());

    @Before
    public void prepareMockDataForService() throws Exception {

        ABCRestClient apiClient = new ABCRestClient(config.getBaseurl() + config.getId() );
        ABCRestClient apiClientSpy=Mockito.spy(apiClient);

        doReturn(getCallResponse()).when(apiClientSpy).callRestMethod(HttpMethod.GET, "");


    }

    @Test
    public void TestPushConfig() throws Exception {
        a.someMethod(StubDataGenerator.getConfig());

    }

    private String getCallResponse() {
        return "{"msg":"sample response"}";
    }

}

我不确定我在这里做错了什么,为什么它调用实际的 callRestMethod 因为我已经创建了一个 spy 。

我也试过用这个Mockito.doReturn(getCallResponse()).when(apiClientSpy.callRestMethod(HttpMethod.GET, ""))

另外,如果我使用Mockito.doReturn()或直接使用doReturn(),这两个语句有什么区别吗?就我而言,两者的行为似乎相同。

在我也尝试过 when().thenReturn(); 之前,但我在某处读到了使用 when().thenReturn() 的地方,当您真正想拨打电话时。如果我的理解有误,请指正。

【问题讨论】:

  • 你是什么意思:“方法内部的方法”?你的测试类甚至可以编译吗?您正在尝试从类外部调用私有方法。
  • 在测试中我遇到了这个调用 pushconfig 的 someMethod 和 M 试图模拟一个在 push config callRestMethod 内部调用的方法
  • 是的,但是在您的测试中您不能调用 pushconfig,因为这是您的 A 类中的私有方法。您需要为那里的 restclient 进行模拟,但由于这是一个局部变量,而不是字段,因此无法模拟。
  • 这些类是我的代码正在编译的示例类。并调用 callRestMethod
  • 您发布的代码无法编译,原因很明显,我之前提到过。由于我提到的另一个原因,您不能模拟该方法调用。

标签: java junit mocking mockito junit4


【解决方案1】:

你可以试试 mock 代替 spy:

   @RunWith(SpringRunner.class)
   @SpringBootTest // (webEnvironment= 
   SpringBootTest.WebEnvironment.RANDOM_PORT)
   public class Test {

    @Autowired
    private A a;



    private Logger logger = LoggerFactory.getLogger(getClass());

    @Before
    public void prepareMockDataForService() throws Exception {

        ABCRestClient apiClientSpy=Mockito.mock(ABCRestClient.class);

        doReturn(getCallResponse()).when(apiClientSpy).callRestMethod(HttpMethod.GET, "");


    }

    @Test
    public void TestPushConfig() throws Exception {
        a.someMethod(StubDataGenerator.getConfig());

    }

    private String getCallResponse() {
        return "{"msg":"sample response"}";
    }
  }

【讨论】:

  • 构造函数参数呢?它仍然调用实际的 callRestMethod
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-24
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
相关资源
最近更新 更多