create dummy request entity object to return while mocking testing request .post methodHow to tell a Mockito mock object to return something different the next time it is called?How to mock private method for testing using PowerMock?Mockito : how to verify method was called on an object created within a method?PowerMockito mock single static method and return objectMocking Objects Created Inside method Under testHow do I mock Spring ApplicationContext's getBean method using Mockito for writing unit tests with TestNG?Mocking a method which returns Page interfaceCannot unmarshall response body with resttemplate jaxbWhy is one object being mocked, while another is not?
Signing using digital signatures?
What do you call the action of someone tackling a stronger person?
Why cruise at 7000' in an A319?
Analog is Obtuse!
Going to get married soon, should I do it on Dec 31 or Jan 1?
Cross over of arrows in a complex diagram
Zombie Diet, why humans
Can gpxpy write .gpx file?
Confusion about multiple information Sets
How can I convince my reader that I will not use a certain trope?
What is the best delay to use between characters sent to the serial port
class enum was not declared in this scope
Should I hide continue button until tasks are completed?
One folder two different locations on ubuntu 18.04
Wilcoxon signed rank test – critical value for n>50
Do sudoku answers always have a single minimal clue set?
How fast can a ship with rotating habitats be accelerated?
How well known and how commonly used was Huffman coding in 1979?
Does anycast addressing add additional latency in any way?
Generate and Graph the Recamán Sequence
Why is Madam Hooch not a professor?
Why isn’t the tax system continuous rather than bracketed?
What's the point of DHS warning passengers about Manila airport?
Should I include salary information on my CV?
create dummy request entity object to return while mocking testing request .post method
How to tell a Mockito mock object to return something different the next time it is called?How to mock private method for testing using PowerMock?Mockito : how to verify method was called on an object created within a method?PowerMockito mock single static method and return objectMocking Objects Created Inside method Under testHow do I mock Spring ApplicationContext's getBean method using Mockito for writing unit tests with TestNG?Mocking a method which returns Page interfaceCannot unmarshall response body with resttemplate jaxbWhy is one object being mocked, while another is not?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
i am trying to mock a request entity objcet for testing a RequestEntity.post method.
RequestEntity<CustomerInfo> body = RequestEntity.post(new
URI(inquiryProperties.getEndCustomer()))
.accept(MediaType.APPLICATION_JSON).body(customerInfo);
i want to mock this method using mockito and in "when " i want to return a dummy object for this.
this is my method i am trying to mock for the controller.
private static final Logger log = LoggerFactory.getLogger(InquiryController.class);
@Autowired
private InquiryProperties inquiryProperties;
@Autowired
private InquiryService inquiryService;
@Autowired
RestTemplate restTemplate;
public static int count = 0;
@Bean
private RestTemplate getRestTemplate()
return new RestTemplate();
public ResponseEntity<List<EndCustomerDTO>> endCustomer(@RequestBody CustomerInfo customerInfo)
throws IOException, JSONException URISyntaxException e)
log.error("InquiryController.endCustomer()" + e.getMessage());
log.info("### END InquiryController.endCustomer() ===>");
if (null == endCustomerDTOs)
return new ResponseEntity<List<EndCustomerDTO>>(new ArrayList<EndCustomerDTO>(), HttpStatus.OK);
return new ResponseEntity<List<EndCustomerDTO>>(endCustomerDTOs, HttpStatus.OK);
java junit mockito powermockito
add a comment |
i am trying to mock a request entity objcet for testing a RequestEntity.post method.
RequestEntity<CustomerInfo> body = RequestEntity.post(new
URI(inquiryProperties.getEndCustomer()))
.accept(MediaType.APPLICATION_JSON).body(customerInfo);
i want to mock this method using mockito and in "when " i want to return a dummy object for this.
this is my method i am trying to mock for the controller.
private static final Logger log = LoggerFactory.getLogger(InquiryController.class);
@Autowired
private InquiryProperties inquiryProperties;
@Autowired
private InquiryService inquiryService;
@Autowired
RestTemplate restTemplate;
public static int count = 0;
@Bean
private RestTemplate getRestTemplate()
return new RestTemplate();
public ResponseEntity<List<EndCustomerDTO>> endCustomer(@RequestBody CustomerInfo customerInfo)
throws IOException, JSONException URISyntaxException e)
log.error("InquiryController.endCustomer()" + e.getMessage());
log.info("### END InquiryController.endCustomer() ===>");
if (null == endCustomerDTOs)
return new ResponseEntity<List<EndCustomerDTO>>(new ArrayList<EndCustomerDTO>(), HttpStatus.OK);
return new ResponseEntity<List<EndCustomerDTO>>(endCustomerDTOs, HttpStatus.OK);
java junit mockito powermockito
Sorry, but it's unclear. What is the code you want to test, and what is the object and the method of that object that you want to mock?
– JB Nizet
Mar 25 at 11:44
i have added the full code
– Apurv Adarsh
Mar 25 at 12:01
But you haven't answered my question.
– JB Nizet
Mar 25 at 12:02
i want to test the end cudtomer method, and for that i need to mock the request entity object and the response object @JB Nizet
– Apurv Adarsh
Mar 26 at 6:19
No. The response object is what you need to test: does your method return the right response. What you need to mock is the restTemplate.exchange() method, or batter, the backend where the rest template sends its request. See baeldung.com/spring-mock-rest-template for the two choices explained with an example
– JB Nizet
Mar 26 at 7:06
add a comment |
i am trying to mock a request entity objcet for testing a RequestEntity.post method.
RequestEntity<CustomerInfo> body = RequestEntity.post(new
URI(inquiryProperties.getEndCustomer()))
.accept(MediaType.APPLICATION_JSON).body(customerInfo);
i want to mock this method using mockito and in "when " i want to return a dummy object for this.
this is my method i am trying to mock for the controller.
private static final Logger log = LoggerFactory.getLogger(InquiryController.class);
@Autowired
private InquiryProperties inquiryProperties;
@Autowired
private InquiryService inquiryService;
@Autowired
RestTemplate restTemplate;
public static int count = 0;
@Bean
private RestTemplate getRestTemplate()
return new RestTemplate();
public ResponseEntity<List<EndCustomerDTO>> endCustomer(@RequestBody CustomerInfo customerInfo)
throws IOException, JSONException URISyntaxException e)
log.error("InquiryController.endCustomer()" + e.getMessage());
log.info("### END InquiryController.endCustomer() ===>");
if (null == endCustomerDTOs)
return new ResponseEntity<List<EndCustomerDTO>>(new ArrayList<EndCustomerDTO>(), HttpStatus.OK);
return new ResponseEntity<List<EndCustomerDTO>>(endCustomerDTOs, HttpStatus.OK);
java junit mockito powermockito
i am trying to mock a request entity objcet for testing a RequestEntity.post method.
RequestEntity<CustomerInfo> body = RequestEntity.post(new
URI(inquiryProperties.getEndCustomer()))
.accept(MediaType.APPLICATION_JSON).body(customerInfo);
i want to mock this method using mockito and in "when " i want to return a dummy object for this.
this is my method i am trying to mock for the controller.
private static final Logger log = LoggerFactory.getLogger(InquiryController.class);
@Autowired
private InquiryProperties inquiryProperties;
@Autowired
private InquiryService inquiryService;
@Autowired
RestTemplate restTemplate;
public static int count = 0;
@Bean
private RestTemplate getRestTemplate()
return new RestTemplate();
public ResponseEntity<List<EndCustomerDTO>> endCustomer(@RequestBody CustomerInfo customerInfo)
throws IOException, JSONException URISyntaxException e)
log.error("InquiryController.endCustomer()" + e.getMessage());
log.info("### END InquiryController.endCustomer() ===>");
if (null == endCustomerDTOs)
return new ResponseEntity<List<EndCustomerDTO>>(new ArrayList<EndCustomerDTO>(), HttpStatus.OK);
return new ResponseEntity<List<EndCustomerDTO>>(endCustomerDTOs, HttpStatus.OK);
java junit mockito powermockito
java junit mockito powermockito
edited Mar 25 at 11:59
Apurv Adarsh
asked Mar 25 at 11:39
Apurv AdarshApurv Adarsh
144 bronze badges
144 bronze badges
Sorry, but it's unclear. What is the code you want to test, and what is the object and the method of that object that you want to mock?
– JB Nizet
Mar 25 at 11:44
i have added the full code
– Apurv Adarsh
Mar 25 at 12:01
But you haven't answered my question.
– JB Nizet
Mar 25 at 12:02
i want to test the end cudtomer method, and for that i need to mock the request entity object and the response object @JB Nizet
– Apurv Adarsh
Mar 26 at 6:19
No. The response object is what you need to test: does your method return the right response. What you need to mock is the restTemplate.exchange() method, or batter, the backend where the rest template sends its request. See baeldung.com/spring-mock-rest-template for the two choices explained with an example
– JB Nizet
Mar 26 at 7:06
add a comment |
Sorry, but it's unclear. What is the code you want to test, and what is the object and the method of that object that you want to mock?
– JB Nizet
Mar 25 at 11:44
i have added the full code
– Apurv Adarsh
Mar 25 at 12:01
But you haven't answered my question.
– JB Nizet
Mar 25 at 12:02
i want to test the end cudtomer method, and for that i need to mock the request entity object and the response object @JB Nizet
– Apurv Adarsh
Mar 26 at 6:19
No. The response object is what you need to test: does your method return the right response. What you need to mock is the restTemplate.exchange() method, or batter, the backend where the rest template sends its request. See baeldung.com/spring-mock-rest-template for the two choices explained with an example
– JB Nizet
Mar 26 at 7:06
Sorry, but it's unclear. What is the code you want to test, and what is the object and the method of that object that you want to mock?
– JB Nizet
Mar 25 at 11:44
Sorry, but it's unclear. What is the code you want to test, and what is the object and the method of that object that you want to mock?
– JB Nizet
Mar 25 at 11:44
i have added the full code
– Apurv Adarsh
Mar 25 at 12:01
i have added the full code
– Apurv Adarsh
Mar 25 at 12:01
But you haven't answered my question.
– JB Nizet
Mar 25 at 12:02
But you haven't answered my question.
– JB Nizet
Mar 25 at 12:02
i want to test the end cudtomer method, and for that i need to mock the request entity object and the response object @JB Nizet
– Apurv Adarsh
Mar 26 at 6:19
i want to test the end cudtomer method, and for that i need to mock the request entity object and the response object @JB Nizet
– Apurv Adarsh
Mar 26 at 6:19
No. The response object is what you need to test: does your method return the right response. What you need to mock is the restTemplate.exchange() method, or batter, the backend where the rest template sends its request. See baeldung.com/spring-mock-rest-template for the two choices explained with an example
– JB Nizet
Mar 26 at 7:06
No. The response object is what you need to test: does your method return the right response. What you need to mock is the restTemplate.exchange() method, or batter, the backend where the rest template sends its request. See baeldung.com/spring-mock-rest-template for the two choices explained with an example
– JB Nizet
Mar 26 at 7:06
add a comment |
1 Answer
1
active
oldest
votes
Sample with Mockito for mocking REST Template Exchange and Spying Response Entity
@RunWith(MockitoJUnitRunner.class)
public class CustomerTest
@Mock
RestTemplate restTemplate;
@Spy
ResponseEntity responseEntity = mock(ResponseEntity.class);
@Inject
InquiryService inquiryService;
@Test
public void endCustomerTest()
EndCustomerDTO yourEndCustomerDTO= new EndCustomerDTO();
//define the entity you want the exchange to return
ResponseEntity<List<EndCustomerDTO >> yourEndCustomerDTOEntity = new ResponseEntity<List<EndCustomerDTO >>(HttpStatus.ACCEPTED);
Mockito.when(restTemplate.exchange(
Matchers.eq("/urlPattern/urlPath"),
Matchers.eq(HttpMethod.POST),
Matchers.<HttpEntity<List<EndCustomerDTO >>>any(),
Matchers.<ParameterizedTypeReference<List<EndCustomerDTO >>>any())
).thenReturn(yourEndCustomerDTOEntity);
//service call to get the end customers
List<EndCustomerDTO > result = inquiryService.getEndCustomers();
//asserting by comparing expected and actual value
Assert.assertEquals(yourEndCustomerDTOEntity , result .get(0));
can you please explain what is "client". and what would client.target mean? and for the path do i need to enter the actual path? and if the path is given , wouldn't it be called
– Apurv Adarsh
Mar 25 at 11:50
@ApurvAdarsh HttpClient is an argument for creating HttpService. Path you can specify the mapped request path. Ideally it would be invoked. Are you trying for an API testing? If so the API should be hit or else we could Mock entire Request/Response.
– Mebin Joe
Mar 25 at 11:54
i'm not doing it for api testing, can you further tell me the steps for mocking the request/response
– Apurv Adarsh
Mar 25 at 11:55
@ApurvAdarsh 2 things. First, Here we are mocking only the Request and hitting API. We are comparing the Response with expected data. Second, Are you intending to mock both Request and Response ? If so there is no use of calling the API and requestEntity.post()
– Mebin Joe
Mar 25 at 11:58
i have added the full code. as this code is not written by me, i cannot change the layout of the code. i have to write the test case for it using mockito
– Apurv Adarsh
Mar 25 at 12:01
|
show 5 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55336987%2fcreate-dummy-request-entity-object-to-return-while-mocking-testing-request-post%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sample with Mockito for mocking REST Template Exchange and Spying Response Entity
@RunWith(MockitoJUnitRunner.class)
public class CustomerTest
@Mock
RestTemplate restTemplate;
@Spy
ResponseEntity responseEntity = mock(ResponseEntity.class);
@Inject
InquiryService inquiryService;
@Test
public void endCustomerTest()
EndCustomerDTO yourEndCustomerDTO= new EndCustomerDTO();
//define the entity you want the exchange to return
ResponseEntity<List<EndCustomerDTO >> yourEndCustomerDTOEntity = new ResponseEntity<List<EndCustomerDTO >>(HttpStatus.ACCEPTED);
Mockito.when(restTemplate.exchange(
Matchers.eq("/urlPattern/urlPath"),
Matchers.eq(HttpMethod.POST),
Matchers.<HttpEntity<List<EndCustomerDTO >>>any(),
Matchers.<ParameterizedTypeReference<List<EndCustomerDTO >>>any())
).thenReturn(yourEndCustomerDTOEntity);
//service call to get the end customers
List<EndCustomerDTO > result = inquiryService.getEndCustomers();
//asserting by comparing expected and actual value
Assert.assertEquals(yourEndCustomerDTOEntity , result .get(0));
can you please explain what is "client". and what would client.target mean? and for the path do i need to enter the actual path? and if the path is given , wouldn't it be called
– Apurv Adarsh
Mar 25 at 11:50
@ApurvAdarsh HttpClient is an argument for creating HttpService. Path you can specify the mapped request path. Ideally it would be invoked. Are you trying for an API testing? If so the API should be hit or else we could Mock entire Request/Response.
– Mebin Joe
Mar 25 at 11:54
i'm not doing it for api testing, can you further tell me the steps for mocking the request/response
– Apurv Adarsh
Mar 25 at 11:55
@ApurvAdarsh 2 things. First, Here we are mocking only the Request and hitting API. We are comparing the Response with expected data. Second, Are you intending to mock both Request and Response ? If so there is no use of calling the API and requestEntity.post()
– Mebin Joe
Mar 25 at 11:58
i have added the full code. as this code is not written by me, i cannot change the layout of the code. i have to write the test case for it using mockito
– Apurv Adarsh
Mar 25 at 12:01
|
show 5 more comments
Sample with Mockito for mocking REST Template Exchange and Spying Response Entity
@RunWith(MockitoJUnitRunner.class)
public class CustomerTest
@Mock
RestTemplate restTemplate;
@Spy
ResponseEntity responseEntity = mock(ResponseEntity.class);
@Inject
InquiryService inquiryService;
@Test
public void endCustomerTest()
EndCustomerDTO yourEndCustomerDTO= new EndCustomerDTO();
//define the entity you want the exchange to return
ResponseEntity<List<EndCustomerDTO >> yourEndCustomerDTOEntity = new ResponseEntity<List<EndCustomerDTO >>(HttpStatus.ACCEPTED);
Mockito.when(restTemplate.exchange(
Matchers.eq("/urlPattern/urlPath"),
Matchers.eq(HttpMethod.POST),
Matchers.<HttpEntity<List<EndCustomerDTO >>>any(),
Matchers.<ParameterizedTypeReference<List<EndCustomerDTO >>>any())
).thenReturn(yourEndCustomerDTOEntity);
//service call to get the end customers
List<EndCustomerDTO > result = inquiryService.getEndCustomers();
//asserting by comparing expected and actual value
Assert.assertEquals(yourEndCustomerDTOEntity , result .get(0));
can you please explain what is "client". and what would client.target mean? and for the path do i need to enter the actual path? and if the path is given , wouldn't it be called
– Apurv Adarsh
Mar 25 at 11:50
@ApurvAdarsh HttpClient is an argument for creating HttpService. Path you can specify the mapped request path. Ideally it would be invoked. Are you trying for an API testing? If so the API should be hit or else we could Mock entire Request/Response.
– Mebin Joe
Mar 25 at 11:54
i'm not doing it for api testing, can you further tell me the steps for mocking the request/response
– Apurv Adarsh
Mar 25 at 11:55
@ApurvAdarsh 2 things. First, Here we are mocking only the Request and hitting API. We are comparing the Response with expected data. Second, Are you intending to mock both Request and Response ? If so there is no use of calling the API and requestEntity.post()
– Mebin Joe
Mar 25 at 11:58
i have added the full code. as this code is not written by me, i cannot change the layout of the code. i have to write the test case for it using mockito
– Apurv Adarsh
Mar 25 at 12:01
|
show 5 more comments
Sample with Mockito for mocking REST Template Exchange and Spying Response Entity
@RunWith(MockitoJUnitRunner.class)
public class CustomerTest
@Mock
RestTemplate restTemplate;
@Spy
ResponseEntity responseEntity = mock(ResponseEntity.class);
@Inject
InquiryService inquiryService;
@Test
public void endCustomerTest()
EndCustomerDTO yourEndCustomerDTO= new EndCustomerDTO();
//define the entity you want the exchange to return
ResponseEntity<List<EndCustomerDTO >> yourEndCustomerDTOEntity = new ResponseEntity<List<EndCustomerDTO >>(HttpStatus.ACCEPTED);
Mockito.when(restTemplate.exchange(
Matchers.eq("/urlPattern/urlPath"),
Matchers.eq(HttpMethod.POST),
Matchers.<HttpEntity<List<EndCustomerDTO >>>any(),
Matchers.<ParameterizedTypeReference<List<EndCustomerDTO >>>any())
).thenReturn(yourEndCustomerDTOEntity);
//service call to get the end customers
List<EndCustomerDTO > result = inquiryService.getEndCustomers();
//asserting by comparing expected and actual value
Assert.assertEquals(yourEndCustomerDTOEntity , result .get(0));
Sample with Mockito for mocking REST Template Exchange and Spying Response Entity
@RunWith(MockitoJUnitRunner.class)
public class CustomerTest
@Mock
RestTemplate restTemplate;
@Spy
ResponseEntity responseEntity = mock(ResponseEntity.class);
@Inject
InquiryService inquiryService;
@Test
public void endCustomerTest()
EndCustomerDTO yourEndCustomerDTO= new EndCustomerDTO();
//define the entity you want the exchange to return
ResponseEntity<List<EndCustomerDTO >> yourEndCustomerDTOEntity = new ResponseEntity<List<EndCustomerDTO >>(HttpStatus.ACCEPTED);
Mockito.when(restTemplate.exchange(
Matchers.eq("/urlPattern/urlPath"),
Matchers.eq(HttpMethod.POST),
Matchers.<HttpEntity<List<EndCustomerDTO >>>any(),
Matchers.<ParameterizedTypeReference<List<EndCustomerDTO >>>any())
).thenReturn(yourEndCustomerDTOEntity);
//service call to get the end customers
List<EndCustomerDTO > result = inquiryService.getEndCustomers();
//asserting by comparing expected and actual value
Assert.assertEquals(yourEndCustomerDTOEntity , result .get(0));
edited Mar 25 at 12:21
answered Mar 25 at 11:46
Mebin JoeMebin Joe
8058 silver badges19 bronze badges
8058 silver badges19 bronze badges
can you please explain what is "client". and what would client.target mean? and for the path do i need to enter the actual path? and if the path is given , wouldn't it be called
– Apurv Adarsh
Mar 25 at 11:50
@ApurvAdarsh HttpClient is an argument for creating HttpService. Path you can specify the mapped request path. Ideally it would be invoked. Are you trying for an API testing? If so the API should be hit or else we could Mock entire Request/Response.
– Mebin Joe
Mar 25 at 11:54
i'm not doing it for api testing, can you further tell me the steps for mocking the request/response
– Apurv Adarsh
Mar 25 at 11:55
@ApurvAdarsh 2 things. First, Here we are mocking only the Request and hitting API. We are comparing the Response with expected data. Second, Are you intending to mock both Request and Response ? If so there is no use of calling the API and requestEntity.post()
– Mebin Joe
Mar 25 at 11:58
i have added the full code. as this code is not written by me, i cannot change the layout of the code. i have to write the test case for it using mockito
– Apurv Adarsh
Mar 25 at 12:01
|
show 5 more comments
can you please explain what is "client". and what would client.target mean? and for the path do i need to enter the actual path? and if the path is given , wouldn't it be called
– Apurv Adarsh
Mar 25 at 11:50
@ApurvAdarsh HttpClient is an argument for creating HttpService. Path you can specify the mapped request path. Ideally it would be invoked. Are you trying for an API testing? If so the API should be hit or else we could Mock entire Request/Response.
– Mebin Joe
Mar 25 at 11:54
i'm not doing it for api testing, can you further tell me the steps for mocking the request/response
– Apurv Adarsh
Mar 25 at 11:55
@ApurvAdarsh 2 things. First, Here we are mocking only the Request and hitting API. We are comparing the Response with expected data. Second, Are you intending to mock both Request and Response ? If so there is no use of calling the API and requestEntity.post()
– Mebin Joe
Mar 25 at 11:58
i have added the full code. as this code is not written by me, i cannot change the layout of the code. i have to write the test case for it using mockito
– Apurv Adarsh
Mar 25 at 12:01
can you please explain what is "client". and what would client.target mean? and for the path do i need to enter the actual path? and if the path is given , wouldn't it be called
– Apurv Adarsh
Mar 25 at 11:50
can you please explain what is "client". and what would client.target mean? and for the path do i need to enter the actual path? and if the path is given , wouldn't it be called
– Apurv Adarsh
Mar 25 at 11:50
@ApurvAdarsh HttpClient is an argument for creating HttpService. Path you can specify the mapped request path. Ideally it would be invoked. Are you trying for an API testing? If so the API should be hit or else we could Mock entire Request/Response.
– Mebin Joe
Mar 25 at 11:54
@ApurvAdarsh HttpClient is an argument for creating HttpService. Path you can specify the mapped request path. Ideally it would be invoked. Are you trying for an API testing? If so the API should be hit or else we could Mock entire Request/Response.
– Mebin Joe
Mar 25 at 11:54
i'm not doing it for api testing, can you further tell me the steps for mocking the request/response
– Apurv Adarsh
Mar 25 at 11:55
i'm not doing it for api testing, can you further tell me the steps for mocking the request/response
– Apurv Adarsh
Mar 25 at 11:55
@ApurvAdarsh 2 things. First, Here we are mocking only the Request and hitting API. We are comparing the Response with expected data. Second, Are you intending to mock both Request and Response ? If so there is no use of calling the API and requestEntity.post()
– Mebin Joe
Mar 25 at 11:58
@ApurvAdarsh 2 things. First, Here we are mocking only the Request and hitting API. We are comparing the Response with expected data. Second, Are you intending to mock both Request and Response ? If so there is no use of calling the API and requestEntity.post()
– Mebin Joe
Mar 25 at 11:58
i have added the full code. as this code is not written by me, i cannot change the layout of the code. i have to write the test case for it using mockito
– Apurv Adarsh
Mar 25 at 12:01
i have added the full code. as this code is not written by me, i cannot change the layout of the code. i have to write the test case for it using mockito
– Apurv Adarsh
Mar 25 at 12:01
|
show 5 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55336987%2fcreate-dummy-request-entity-object-to-return-while-mocking-testing-request-post%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Sorry, but it's unclear. What is the code you want to test, and what is the object and the method of that object that you want to mock?
– JB Nizet
Mar 25 at 11:44
i have added the full code
– Apurv Adarsh
Mar 25 at 12:01
But you haven't answered my question.
– JB Nizet
Mar 25 at 12:02
i want to test the end cudtomer method, and for that i need to mock the request entity object and the response object @JB Nizet
– Apurv Adarsh
Mar 26 at 6:19
No. The response object is what you need to test: does your method return the right response. What you need to mock is the restTemplate.exchange() method, or batter, the backend where the rest template sends its request. See baeldung.com/spring-mock-rest-template for the two choices explained with an example
– JB Nizet
Mar 26 at 7:06