【问题标题】:java spring boot, how to write a test to check send requestjava spring boot,如何编写测试来检查发送请求
【发布时间】:2021-11-15 00:12:33
【问题描述】:

谁能告诉我你怎么写一个测试。我现在必须使用RestTemplate 测试从一台服务器向另一台服务器发送请求。

class ServiceTest {

@Mock
private RestTemplate restTemplate = new RestTemplate();

@InjectMocks
private RConsumerService refundConsumerService = new RConsumerService(new RestTemplateBuilder());

@Test
public void sendRequestToBillingService(){
    ChargeResponse chargeResponse = new ChargeResponse();
    chargeResponse.setInstanceKey("testInstanceKey");

    KafkaMessage kafkaMessage = new KafkaMessage();
    kafkaMessage.setApplication_id(1L);
    kafkaMessage.setCompany_id(1111);
    TransactionRequestContext reqContext = refundConsumerService.createTxnRequestContext(kafkaMessage);

    Mockito.when(restTemplate.postForEntity(Mockito.any()
            , refundConsumerService.buildChargeRequest(reqContext), ChargeResponse.class))
            .thenReturn(new ResponseEntity<>(chargeResponse, HttpStatus.OK));

    refundConsumerService.refund(kafkaMessage);
    assertEquals(chargeResponse.getInstanceKey(), "testInstanceKey");
}

}

如何正确地写条件

Mockito.when(restTemplate.postForEntity(Mockito.any()
            , refundConsumerService.buildChargeRequest(reqContext), ChargeResponse.class))
            .thenReturn(new ResponseEntity<>(chargeResponse, HttpStatus.OK));

现在我收到了这个异常java.lang.IllegalArgumentException: URI is required

【问题讨论】:

    标签: java junit mockito junit5


    【解决方案1】:

    当您使用@Mock@InjectMocks 时,您不需要创建这些对象的new 实例。 Mockito 将为您注入它。我猜你有这个异常是因为这个参数:Mockito.any() 在你的Mockito.when() 中。它必须是 Uri 类型。

    您的代码将如下所示:

    @ExtendWith(MockitoExtension.class)
    class ServiceTest {
    
        @Mock
        private RestTemplate restTemplate;
    
        @InjectMocks
        private RConsumerService refundConsumerService;
    
        @Test
        public void sendRequestToBillingService() {
            ChargeResponse chargeResponse = new ChargeResponse();
            chargeResponse.setInstanceKey("testInstanceKey");
    
            KafkaMessage kafkaMessage = new KafkaMessage();
            kafkaMessage.setApplication_id(1L);
            kafkaMessage.setCompany_id(1111);
            TransactionRequestContext reqContext = refundConsumerService.createTxnRequestContext(kafkaMessage);
    
            URI mockUri = URI.create("http://localhost/mockUri");
            Mockito.when(restTemplate.postForEntity(mockUri
                            , refundConsumerService.buildChargeRequest(reqContext), ChargeResponse.class))
                    .thenReturn(new ResponseEntity<>(chargeResponse, HttpStatus.OK));
    
            refundConsumerService.refund(kafkaMessage);
            assertEquals(chargeResponse.getInstanceKey(), "testInstanceKey");
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 2019-07-07
      相关资源
      最近更新 更多