简体   繁体   English

java spring boot,如何编写测试来检查发送请求

[英]java spring boot, how to write a test to check send request

Tell me how you can write a test.告诉我如何编写测试。 I now have to test sending a request from one server to another using RestTemplate.我现在必须使用 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");
}

} }

How do I write the condition correctly in Mockito.when(restTemplate.postForEntity(Mockito.any() , refundConsumerService.buildChargeRequest(reqContext), ChargeResponse.class)) .thenReturn(new ResponseEntity<>(chargeResponse, HttpStatus.OK));如何在 Mockito.when(restTemplate.postForEntity(Mockito.any() ,refundConsumerService.buildChargeRequest(reqContext), ChargeResponse.class)) .thenReturn(new ResponseEntity<>(chargeResponse, HttpStatus.OK)); ? ?

Now I am getting this exception java.lang.IllegalArgumentException: URI is required现在我得到这个异常 java.lang.IllegalArgumentException: URI is required

As you are using @Mock and @InjectMocks , you don't need to create new instance of those objects.当您使用@Mock@InjectMocks ,您不需要创建这些对象的new实例。 Mockito will inject it for you. Mockito会为你注入它。 I guess you have this exception because of this parameter : Mockito.any() in your Mockito.when() .我想你有这样的例外,因为这个参数: Mockito.any()在你的Mockito.when() It have to be of a Uri type.它必须是Uri类型。

Your code will looks like this :您的代码将如下所示:

@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");
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM