gpt4 book ai didi

java - 如何在 RestTemplate 上应用 PowerMockito

转载 作者:行者123 更新时间:2023-12-01 21:26:15 25 4
gpt4 key购买 nike

在我的代码中,我有 RestTemplate 的 New 运算符。让我知道如何在下面的 RestTemplate 上应用 Powermockito。

ResponseEntity<String> response =  new RestTemplate().exchange(requestEntity, String.class);

最佳答案

您需要使用PowerMockito.whenNew并使用mock返回模拟对象

参见example使用 PowerMockDemo 类和 Point new 运算符代替 RestTemplate

annotate unit test with @RunWith(PowerMockRunner.class) telling JUnit to use PowerMock runner and with @PrepareForTest(PowerMockDemo.class) telling PowerMock to get inside PowerMockDemo class and prepare it for mocking. Mocking is done with PowerMockito.whenNew(Point.class).withAnyArguments().thenReturn(mockPoint). It tells PowerMock when a new object from class Point is instantiated with whatever arguments to return mockPoint instead. It is possible to return different objects based on different arguments Point is created with withArguments() method

关于java - 如何在 RestTemplate 上应用 PowerMockito,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58831745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com