Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有办法模拟本地对象上的方法调用_Java_Spring Boot_Junit_Mockito_Spring Boot Test - Fatal编程技术网

Java 有没有办法模拟本地对象上的方法调用

Java 有没有办法模拟本地对象上的方法调用,java,spring-boot,junit,mockito,spring-boot-test,Java,Spring Boot,Junit,Mockito,Spring Boot Test,我正在使用Mockito框架为现有的spring启动项目编写测试用例 在其中一个类中,他们在函数中创建了rest模板的本地实例,而不是@Autowiring public LatxDetails getLatxCaseDetail_Fallback(String endpoint, Map<String, String> requestMap) { //some code RestTemplate restTemplate = new RestTemplate(); Respo

我正在使用Mockito框架为现有的spring启动项目编写测试用例

在其中一个类中,他们在函数中创建了
rest模板的本地实例,而不是
@Autowiring

public LatxDetails getLatxCaseDetail_Fallback(String endpoint, Map<String, String> requestMap) {

//some code

RestTemplate restTemplate = new RestTemplate(); 
ResponseEntity<String> response = restTemplate.exchange(kxCreateEndpoint, HttpMethod.POST, httpEntity, String.class);

//some code
public LatxDetails getLatxCaseDetail\u回退(字符串端点,映射请求映射){
//一些代码
RestTemplate RestTemplate=新RestTemplate();
ResponseEntity response=restemplate.exchange(kxCreateEndpoint,HttpMethod.POST,httpEntity,String.class);
//一些代码

我无法模拟rest调用。我无法添加任何其他外部JAR或更改代码。我应该如何继续?非常感谢您的帮助。

用模拟替换您的RestTemplate或创建它的间谍:

// Mock
RestTemplate restTemplate = Mockito.mock(RestTemplate.class);

// Spy
RestTemplate restTemplate = new RestTemplate(); 
RestTemplate spy = Mockito.spy(restTemplate);
您可以像往常一样模拟
exchange
方法:

// mock
doReturn(...).when(restTemplate).exchange(...)

// spy
doReturn(...).when(spy).exchange(...)

将RestTemplate替换为模拟或创建其间谍:

// Mock
RestTemplate restTemplate = Mockito.mock(RestTemplate.class);

// Spy
RestTemplate restTemplate = new RestTemplate(); 
RestTemplate spy = Mockito.spy(restTemplate);
您可以像往常一样模拟
exchange
方法:

// mock
doReturn(...).when(restTemplate).exchange(...)

// spy
doReturn(...).when(spy).exchange(...)

显而易见的解决方案是注入
RestTemplate
,而不是在本地实例化一个,但正如您所说,您不能更改现有代码,我建议使用该方法替换
RestTemplate
的构造函数调用

例如

然后在restTemplateMock对象上进行
设置时执行

正如Strelok在评论中提到的:为了使用PowerMockito,您需要通过使用
@RunWith(PowerMockRunner.class)
@PrepareForTest
注释测试类,使用
PowerMockRunner
运行测试

例如


显而易见的解决方案是注入
RestTemplate
,而不是在本地实例化一个,但正如您所说,您不能更改现有代码,我建议使用该方法替换
RestTemplate
的构造函数调用

例如

然后在restTemplateMock对象上进行
设置时执行

正如Strelok在评论中提到的:为了使用PowerMockito,您需要通过使用
@RunWith(PowerMockRunner.class)
@PrepareForTest
注释测试类,使用
PowerMockRunner
运行测试

例如


也许您可以扩展您的答案,以注意您必须使用PowerMockitorRunner运行测试,关键是您必须使用@PrepareForTest(创建NewInstance.class的类)注释测试。这将为不知道PowerMockito如何工作的人省去很多麻烦。也许您可以扩展您的答案,以注意您必须使用PowerMockitorRunner运行测试,关键是您必须使用@PrepareForTest(创建NewInstance.class的类)注释测试.这将为不知道PowerMockito如何工作的人省去很多头痛。