mock a request entity call for unit testHow do you assert that a certain exception is thrown in JUnit 4 tests?How do I call one constructor from another in Java?Is an entity body allowed for an HTTP DELETE request?HTTP GET with request bodyHow to make mock to void methods with MockitoHow to use java.net.URLConnection to fire and handle HTTP requestsIs null check needed before calling instanceof?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?How to verify that a specific method was not called using Mockito?Difference between @Mock and @InjectMocks
What is the highest power supply a Raspberry pi 3 B can handle without getting damaged?
Are intrusions within a foreign embassy considered an act of war?
How is the idea of "girlfriend material" naturally expressed in Russian?
Are there examples of rowers who also fought?
My student in one course asks for paid tutoring in another course. Appropriate?
"Correct me if I'm wrong"
「捨ててしまう」why is there two て’s used here?
In the US, can a former president run again?
How can a warlock learn from a spellbook?
Story of a Witch Boy
Is using legacy mode instead of UEFI mode a bad thing to do?
Leaving job close to major deadlines
Why is it 出差去 and not 去出差?
In a list with unique pairs A, B, how can I sort them so that the last B is the first A in the next pair?
Parse JSON in LWC
How to write a nice frame challenge?
Pin support, why is there no horizontal reaction force?
What does this Swiss black on yellow rectangular traffic sign with a symbol looking like a dart mean?
Is there a polite way to ask about one's ethnicity?
Synaptic Static - when to roll the d6?
Is declining an undergraduate award which causes me discomfort appropriate?
What is that ceiling compartment of a Boeing 737?
Are there any individual aliens that have gained superpowers in the Marvel universe?
Scaling an object to change its key
mock a request entity call for unit test
How do you assert that a certain exception is thrown in JUnit 4 tests?How do I call one constructor from another in Java?Is an entity body allowed for an HTTP DELETE request?HTTP GET with request bodyHow to make mock to void methods with MockitoHow to use java.net.URLConnection to fire and handle HTTP requestsIs null check needed before calling instanceof?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?How to verify that a specific method was not called using Mockito?Difference between @Mock and @InjectMocks
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
i want to mock the request entity and response to test the method on the controller method, this code has been written by another developer and i am supposed to test it using mockito.i'm mocking the controller class
i am trying to mock the request entity value and the respionse entity value , but it's not working and i'm getting a reflection error when i'm trying to debug
public class InquiryController {
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();
@PostMapping(value = "/endCustomer", produces = MediaType.APPLICATION_JSON_VALUE , consumes =
MediaType.APPLICATION_JSON_VALUE )
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 rest junit mockito resttemplate
add a comment |
i want to mock the request entity and response to test the method on the controller method, this code has been written by another developer and i am supposed to test it using mockito.i'm mocking the controller class
i am trying to mock the request entity value and the respionse entity value , but it's not working and i'm getting a reflection error when i'm trying to debug
public class InquiryController {
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();
@PostMapping(value = "/endCustomer", produces = MediaType.APPLICATION_JSON_VALUE , consumes =
MediaType.APPLICATION_JSON_VALUE )
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 rest junit mockito resttemplate
It looks like you're... trying to mix a configuration and a controller? Replace your field injection with constructor injection and use theRestOperations
interface instead ofRestTemplate
, and you'll be able to mock the dependency trivially.
– chrylis
Mar 25 at 6:15
add a comment |
i want to mock the request entity and response to test the method on the controller method, this code has been written by another developer and i am supposed to test it using mockito.i'm mocking the controller class
i am trying to mock the request entity value and the respionse entity value , but it's not working and i'm getting a reflection error when i'm trying to debug
public class InquiryController {
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();
@PostMapping(value = "/endCustomer", produces = MediaType.APPLICATION_JSON_VALUE , consumes =
MediaType.APPLICATION_JSON_VALUE )
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 rest junit mockito resttemplate
i want to mock the request entity and response to test the method on the controller method, this code has been written by another developer and i am supposed to test it using mockito.i'm mocking the controller class
i am trying to mock the request entity value and the respionse entity value , but it's not working and i'm getting a reflection error when i'm trying to debug
public class InquiryController {
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();
@PostMapping(value = "/endCustomer", produces = MediaType.APPLICATION_JSON_VALUE , consumes =
MediaType.APPLICATION_JSON_VALUE )
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 rest junit mockito resttemplate
java rest junit mockito resttemplate
asked Mar 25 at 6:13
Apurv AdarshApurv Adarsh
144
144
It looks like you're... trying to mix a configuration and a controller? Replace your field injection with constructor injection and use theRestOperations
interface instead ofRestTemplate
, and you'll be able to mock the dependency trivially.
– chrylis
Mar 25 at 6:15
add a comment |
It looks like you're... trying to mix a configuration and a controller? Replace your field injection with constructor injection and use theRestOperations
interface instead ofRestTemplate
, and you'll be able to mock the dependency trivially.
– chrylis
Mar 25 at 6:15
It looks like you're... trying to mix a configuration and a controller? Replace your field injection with constructor injection and use the
RestOperations
interface instead of RestTemplate
, and you'll be able to mock the dependency trivially.– chrylis
Mar 25 at 6:15
It looks like you're... trying to mix a configuration and a controller? Replace your field injection with constructor injection and use the
RestOperations
interface instead of RestTemplate
, and you'll be able to mock the dependency trivially.– chrylis
Mar 25 at 6:15
add a comment |
2 Answers
2
active
oldest
votes
It's because instance of RestTemplate
is not injected through Spring IOC
when you do the REST call. You need to declare the getRestTemplate
method of in the component class which is scanned during application startup or in other words during component scan. Thus making restTemplate
available for autowire
.
add a comment |
Once you separate the config from the controller as @chrylis suggested, you proceed further like this.
You must be trying to mock the RequestEntity.post method. Note that it is a static method and is mocked a bit differently than the usual public instance methods. For this, you need to use PowerMockito as Mockito won't do.
add the dependency in pom like this:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
then anotate the test class with @RunWith
, and @PrepareForTest
like so:
@RunWith(PowerMockRunner.class)
@PrepareForTest(RequestEntity.class)
public class TestClass
and the mock the post method as so:
PowerMockito.mockStatic(RequestEntity.class); when(RequestEntity.post(any(URI.class))).thenReturn(getRequestEntityResponseBody());
private RequestEntity< CustomerInfo > getRequestEntityResponseBody()
//code
UPDATE
CustomerInfo customerInfo = new CustomerInfo();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK);
PowerMockito.mockStatic(RequestEntity.class);
when(RequestEntity.post(any(URI.class))).thenReturn(customerInfoRequestEntity);
1
can you explain why you hav returned the getRequestEntityResponseBody() and why have you written that method @raviiii1
– Apurv Adarsh
Mar 25 at 6:56
It is possible that there are multiple tests for the same method. In that case, this can be reused to return RequestEntity<CustomerInfo> instead of repeating the creation in each test. It's perfectly fine if you create the objects locally.
– raviiii1
Mar 25 at 7:38
can you show me an exapmle of creating an object /mock object locally. i'm facing issues as it requires url and its a mock test @raviiii1
– Apurv Adarsh
Mar 25 at 9:33
@ApurvAdarsh , I have updated the response. As I mentioned before, the creation ofcustomerInfoRequestEntity
can be put into a method so that it can be used in other tests.
– raviiii1
Mar 25 at 9:45
can we create request entity object using response entity? RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK); @raviiii1
– Apurv Adarsh
Mar 25 at 11:32
add a comment |
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%2f55332139%2fmock-a-request-entity-call-for-unit-test%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's because instance of RestTemplate
is not injected through Spring IOC
when you do the REST call. You need to declare the getRestTemplate
method of in the component class which is scanned during application startup or in other words during component scan. Thus making restTemplate
available for autowire
.
add a comment |
It's because instance of RestTemplate
is not injected through Spring IOC
when you do the REST call. You need to declare the getRestTemplate
method of in the component class which is scanned during application startup or in other words during component scan. Thus making restTemplate
available for autowire
.
add a comment |
It's because instance of RestTemplate
is not injected through Spring IOC
when you do the REST call. You need to declare the getRestTemplate
method of in the component class which is scanned during application startup or in other words during component scan. Thus making restTemplate
available for autowire
.
It's because instance of RestTemplate
is not injected through Spring IOC
when you do the REST call. You need to declare the getRestTemplate
method of in the component class which is scanned during application startup or in other words during component scan. Thus making restTemplate
available for autowire
.
answered Mar 25 at 6:21
Shubhendu PramanikShubhendu Pramanik
2,4702820
2,4702820
add a comment |
add a comment |
Once you separate the config from the controller as @chrylis suggested, you proceed further like this.
You must be trying to mock the RequestEntity.post method. Note that it is a static method and is mocked a bit differently than the usual public instance methods. For this, you need to use PowerMockito as Mockito won't do.
add the dependency in pom like this:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
then anotate the test class with @RunWith
, and @PrepareForTest
like so:
@RunWith(PowerMockRunner.class)
@PrepareForTest(RequestEntity.class)
public class TestClass
and the mock the post method as so:
PowerMockito.mockStatic(RequestEntity.class); when(RequestEntity.post(any(URI.class))).thenReturn(getRequestEntityResponseBody());
private RequestEntity< CustomerInfo > getRequestEntityResponseBody()
//code
UPDATE
CustomerInfo customerInfo = new CustomerInfo();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK);
PowerMockito.mockStatic(RequestEntity.class);
when(RequestEntity.post(any(URI.class))).thenReturn(customerInfoRequestEntity);
1
can you explain why you hav returned the getRequestEntityResponseBody() and why have you written that method @raviiii1
– Apurv Adarsh
Mar 25 at 6:56
It is possible that there are multiple tests for the same method. In that case, this can be reused to return RequestEntity<CustomerInfo> instead of repeating the creation in each test. It's perfectly fine if you create the objects locally.
– raviiii1
Mar 25 at 7:38
can you show me an exapmle of creating an object /mock object locally. i'm facing issues as it requires url and its a mock test @raviiii1
– Apurv Adarsh
Mar 25 at 9:33
@ApurvAdarsh , I have updated the response. As I mentioned before, the creation ofcustomerInfoRequestEntity
can be put into a method so that it can be used in other tests.
– raviiii1
Mar 25 at 9:45
can we create request entity object using response entity? RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK); @raviiii1
– Apurv Adarsh
Mar 25 at 11:32
add a comment |
Once you separate the config from the controller as @chrylis suggested, you proceed further like this.
You must be trying to mock the RequestEntity.post method. Note that it is a static method and is mocked a bit differently than the usual public instance methods. For this, you need to use PowerMockito as Mockito won't do.
add the dependency in pom like this:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
then anotate the test class with @RunWith
, and @PrepareForTest
like so:
@RunWith(PowerMockRunner.class)
@PrepareForTest(RequestEntity.class)
public class TestClass
and the mock the post method as so:
PowerMockito.mockStatic(RequestEntity.class); when(RequestEntity.post(any(URI.class))).thenReturn(getRequestEntityResponseBody());
private RequestEntity< CustomerInfo > getRequestEntityResponseBody()
//code
UPDATE
CustomerInfo customerInfo = new CustomerInfo();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK);
PowerMockito.mockStatic(RequestEntity.class);
when(RequestEntity.post(any(URI.class))).thenReturn(customerInfoRequestEntity);
1
can you explain why you hav returned the getRequestEntityResponseBody() and why have you written that method @raviiii1
– Apurv Adarsh
Mar 25 at 6:56
It is possible that there are multiple tests for the same method. In that case, this can be reused to return RequestEntity<CustomerInfo> instead of repeating the creation in each test. It's perfectly fine if you create the objects locally.
– raviiii1
Mar 25 at 7:38
can you show me an exapmle of creating an object /mock object locally. i'm facing issues as it requires url and its a mock test @raviiii1
– Apurv Adarsh
Mar 25 at 9:33
@ApurvAdarsh , I have updated the response. As I mentioned before, the creation ofcustomerInfoRequestEntity
can be put into a method so that it can be used in other tests.
– raviiii1
Mar 25 at 9:45
can we create request entity object using response entity? RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK); @raviiii1
– Apurv Adarsh
Mar 25 at 11:32
add a comment |
Once you separate the config from the controller as @chrylis suggested, you proceed further like this.
You must be trying to mock the RequestEntity.post method. Note that it is a static method and is mocked a bit differently than the usual public instance methods. For this, you need to use PowerMockito as Mockito won't do.
add the dependency in pom like this:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
then anotate the test class with @RunWith
, and @PrepareForTest
like so:
@RunWith(PowerMockRunner.class)
@PrepareForTest(RequestEntity.class)
public class TestClass
and the mock the post method as so:
PowerMockito.mockStatic(RequestEntity.class); when(RequestEntity.post(any(URI.class))).thenReturn(getRequestEntityResponseBody());
private RequestEntity< CustomerInfo > getRequestEntityResponseBody()
//code
UPDATE
CustomerInfo customerInfo = new CustomerInfo();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK);
PowerMockito.mockStatic(RequestEntity.class);
when(RequestEntity.post(any(URI.class))).thenReturn(customerInfoRequestEntity);
Once you separate the config from the controller as @chrylis suggested, you proceed further like this.
You must be trying to mock the RequestEntity.post method. Note that it is a static method and is mocked a bit differently than the usual public instance methods. For this, you need to use PowerMockito as Mockito won't do.
add the dependency in pom like this:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
then anotate the test class with @RunWith
, and @PrepareForTest
like so:
@RunWith(PowerMockRunner.class)
@PrepareForTest(RequestEntity.class)
public class TestClass
and the mock the post method as so:
PowerMockito.mockStatic(RequestEntity.class); when(RequestEntity.post(any(URI.class))).thenReturn(getRequestEntityResponseBody());
private RequestEntity< CustomerInfo > getRequestEntityResponseBody()
//code
UPDATE
CustomerInfo customerInfo = new CustomerInfo();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK);
PowerMockito.mockStatic(RequestEntity.class);
when(RequestEntity.post(any(URI.class))).thenReturn(customerInfoRequestEntity);
edited Mar 25 at 9:42
answered Mar 25 at 6:29
raviiii1raviiii1
611516
611516
1
can you explain why you hav returned the getRequestEntityResponseBody() and why have you written that method @raviiii1
– Apurv Adarsh
Mar 25 at 6:56
It is possible that there are multiple tests for the same method. In that case, this can be reused to return RequestEntity<CustomerInfo> instead of repeating the creation in each test. It's perfectly fine if you create the objects locally.
– raviiii1
Mar 25 at 7:38
can you show me an exapmle of creating an object /mock object locally. i'm facing issues as it requires url and its a mock test @raviiii1
– Apurv Adarsh
Mar 25 at 9:33
@ApurvAdarsh , I have updated the response. As I mentioned before, the creation ofcustomerInfoRequestEntity
can be put into a method so that it can be used in other tests.
– raviiii1
Mar 25 at 9:45
can we create request entity object using response entity? RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK); @raviiii1
– Apurv Adarsh
Mar 25 at 11:32
add a comment |
1
can you explain why you hav returned the getRequestEntityResponseBody() and why have you written that method @raviiii1
– Apurv Adarsh
Mar 25 at 6:56
It is possible that there are multiple tests for the same method. In that case, this can be reused to return RequestEntity<CustomerInfo> instead of repeating the creation in each test. It's perfectly fine if you create the objects locally.
– raviiii1
Mar 25 at 7:38
can you show me an exapmle of creating an object /mock object locally. i'm facing issues as it requires url and its a mock test @raviiii1
– Apurv Adarsh
Mar 25 at 9:33
@ApurvAdarsh , I have updated the response. As I mentioned before, the creation ofcustomerInfoRequestEntity
can be put into a method so that it can be used in other tests.
– raviiii1
Mar 25 at 9:45
can we create request entity object using response entity? RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK); @raviiii1
– Apurv Adarsh
Mar 25 at 11:32
1
1
can you explain why you hav returned the getRequestEntityResponseBody() and why have you written that method @raviiii1
– Apurv Adarsh
Mar 25 at 6:56
can you explain why you hav returned the getRequestEntityResponseBody() and why have you written that method @raviiii1
– Apurv Adarsh
Mar 25 at 6:56
It is possible that there are multiple tests for the same method. In that case, this can be reused to return RequestEntity<CustomerInfo> instead of repeating the creation in each test. It's perfectly fine if you create the objects locally.
– raviiii1
Mar 25 at 7:38
It is possible that there are multiple tests for the same method. In that case, this can be reused to return RequestEntity<CustomerInfo> instead of repeating the creation in each test. It's perfectly fine if you create the objects locally.
– raviiii1
Mar 25 at 7:38
can you show me an exapmle of creating an object /mock object locally. i'm facing issues as it requires url and its a mock test @raviiii1
– Apurv Adarsh
Mar 25 at 9:33
can you show me an exapmle of creating an object /mock object locally. i'm facing issues as it requires url and its a mock test @raviiii1
– Apurv Adarsh
Mar 25 at 9:33
@ApurvAdarsh , I have updated the response. As I mentioned before, the creation of
customerInfoRequestEntity
can be put into a method so that it can be used in other tests.– raviiii1
Mar 25 at 9:45
@ApurvAdarsh , I have updated the response. As I mentioned before, the creation of
customerInfoRequestEntity
can be put into a method so that it can be used in other tests.– raviiii1
Mar 25 at 9:45
can we create request entity object using response entity? RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK); @raviiii1
– Apurv Adarsh
Mar 25 at 11:32
can we create request entity object using response entity? RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK); @raviiii1
– Apurv Adarsh
Mar 25 at 11:32
add a comment |
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%2f55332139%2fmock-a-request-entity-call-for-unit-test%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
It looks like you're... trying to mix a configuration and a controller? Replace your field injection with constructor injection and use the
RestOperations
interface instead ofRestTemplate
, and you'll be able to mock the dependency trivially.– chrylis
Mar 25 at 6:15