英文:
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<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.
答案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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论