模拟的Spring bean restTemplate方法返回null

时间:2019-03-05 21:30:01

标签: java spring spring-boot mockito

我正在使用Spring Boot应用程序,该应用程序从服务层进行了三个外部api调用,因此在编写集成测试时,我试图用模拟的RestTemplate bean和在进行三个外部API调用时,我指定要返回一些数据

RestTemplate

但是问题是,无论何时调用given(this.restTemplate.exchange(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.isA(new ParameterizedTypeReference<TestData1>() { }.getClass()))).willReturn(ResponseEntity.accepted().body(new TestData1())); given(this.restTemplate.exchange(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.isA(new ParameterizedTypeReference<TestData2>() { }.getClass()))).willReturn(ResponseEntity.accepted().body(new TestData2())); given(this.restTemplate.exchange(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.isA(new ParameterizedTypeReference<List<TestData3>>() { }.getClass()))).willReturn(ResponseEntity.accepted().body(Arrays.asList(new TestData3()))); 方法,它都会返回restTemplate.exchange作为响应,而不是返回具有相应主体的null

TestConfig

ResponseEntity

TestClass

@TestConfiguration
@ActiveProfiles("test")
@Profile("test")
public class TestConfig {

     @Bean
     public RestTemplate restTemplae() {
         return Mockito.mock(RestTemplate.class);
      }

0 个答案:

没有答案
相关问题