ResponseEntity在RestTemplate的JUNIT调用中返回为null。

huangapple go评论71阅读模式
英文:

ResponseEntity Coming as null in Resttemplate JUNIT Call

问题

Writing JUNIT-5 for a GET REST Call
My actual implementation is like below and its working fine.

    ResponseEntity<OrderDocument> responseEntity = restTemplate.exchange(
    URL, HttpMethod.GET, new HttpEntity<>(headers), OrderDocument.class,
    message.getPayload().toString());
    responseEntity.getBody() // Null pointer Exception when calling from JUNIT

My Full implementation of JUNIT is below

    @SpringBootTest
    @ActiveProfiles("test")
    class OrderMasterClientImplTest {

    @Autowired
    OrderMasterClientImpl orderMasterClient;
    private ConsumerMessage consumerMessage;
    private EventMessage eventMessage;

    @MockBean
    @Qualifier("orderMasterRestTemplate")
    private RestTemplate restTemplate;

    @MockBean
    ResponseEntity responseEntity;

    @BeforeEach
    public void setUp() throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        eventMessage = objectMapper.readValue(
                this.getClass().getClassLoader().getResourceAsStream(
                        "event_message.json"),
                EventMessage.class);
        System.out.println(" " + eventMessage.getOrderId());
        consumerMessage = new ConsumerMessage(eventMessage);
        System.out.println(" consumerMessage " + consumerMessage.getPayload());

    }
    @Test
    void consume() {

        when(restTemplate.exchange(anyString(), any(HttpMethod.class),
    any(HttpEntity.class), any(Class.class), any(String.class))
        ).thenReturn(responseEntity);

        System.out.println(" consumerMessage " + consumerMessage.getPayload());
        OrderDocument orderDocumentActual =
    orderMasterClient.consume(consumerMessage);

        Assertions.assertNotNull(orderDocumentActual);
    }
}
 
 What is the mistake I am doing. Please help.
英文:

Writing JUNIT-5 for a GET REST Call
My actual implementation is like below and its working fine.

ResponseEntity&lt;OrderDocument&gt; responseEntity = restTemplate.exchange(
URL,HttpMethod.GET,new HttpEntity&lt;&gt;(headers),OrderDocument.class,
message.getPayload().toString());
responseEntity.getBody() // Null pointer Exception when calling from JUNIT

My Full implementation of JUNIT is below

@SpringBootTest
@ActiveProfiles(&quot;test&quot;)
class OrderMasterClientImplTest {

@Autowired
OrderMasterClientImpl orderMasterClient;
private ConsumerMessage consumerMessage;
private EventMessage eventMessage;

@MockBean
@Qualifier(&quot;orderMasterRestTemplate&quot;)
private RestTemplate restTemplate;

@MockBean
ResponseEntity responseEntity;

@BeforeEach
public void setUp() throws Exception {
    ObjectMapper objectMapper= new ObjectMapper();
    eventMessage = objectMapper.readValue(
            this.getClass().getClassLoader().getResourceAsStream(
                    &quot;event_message.json&quot;),
            EventMessage.class);
    System.out.println( &quot; &quot;+eventMessage.getOrderId());
    consumerMessage = new ConsumerMessage(eventMessage);
    System.out.println( &quot; consumerMessage &quot;+consumerMessage.getPayload());

}
@Test
void consume() {

    when(restTemplate.exchange(anyString() , any(HttpMethod.class)   ,  
any(HttpEntity.class) ,any(Class.class) , any(String.class) 
)).thenReturn(responseEntity);

    System.out.println( &quot; consumerMessage &quot;+consumerMessage.getPayload());
    OrderDocument orderDocumentactual = 
orderMasterClient.consume(consumerMessage);

    Assertions.assertNotNull(orderDocumentactual);
}

}

What is the mistake i am doing . Please help.

答案1

得分: 1

在你的存根中,尝试使用any(OrderDocument.class),而不是any(Class.class)

英文:

In your stubbing, try usingany(OrderDocument.class) instead of any(Class.class)

答案2

得分: 1

用这种方式进行模拟restTemplate的测试,当服务器返回除了实际成功响应以外的任何内容时,可能会导致意外行为。我建议使用MockRestServiceServer,因为这样可以提供更好的测试。

如果您想继续当前的方法 - 您似乎正在返回一个由@MockBean注解创建的responseEntity模拟对象。我在您的问题中没有看到任何代码定义了当有人调用其getBody()方法时,这个模拟对象的行为。这可能是需要考虑的问题。您可以定义一个真实的ResponseEntity来在您的模拟中返回,或者模拟与responseEntity的所有交互。

英文:

Testing in this fashion with mocking a restTemplate can lead to unexpected behaviour when the server returns anything other than an actual successfull response. I'd recommend using MockRestServiceServer as this provides for better testing.

If you do want to continue the current approach - you seem to be returning a responseEntity which is a mock created by the @MockBean annotation. I don't see any code in your question which defines how that mock will behave when someone calls a getBody() on it. That could be something to look at. You could either define a real ResponseEntity to be returned in your mock, or mock all interactions with responseEntity.

huangapple
  • 本文由 发表于 2020年4月11日 11:40:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61151837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定