0
0
Spring Bootframework~10 mins

TestRestTemplate for full integration in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to autowire TestRestTemplate in a Spring Boot test class.

Spring Boot
@Autowired
private [1] restTemplate;
Drag options to blanks, or click blank then click option'
ATestRestTemplate
BWebClient
CRestTemplate
DHttpClient
Attempts:
3 left
💡 Hint
Common Mistakes
Using RestTemplate instead of TestRestTemplate
Using WebClient which is reactive
Using HttpClient which is not Spring-specific
2fill in blank
medium

Complete the code to send a GET request to '/api/items' using TestRestTemplate.

Spring Boot
ResponseEntity<String> response = restTemplate.[1]("/api/items");
Drag options to blanks, or click blank then click option'
AgetForEntity
BpostForEntity
Cexchange
Dput
Attempts:
3 left
💡 Hint
Common Mistakes
Using postForEntity for GET requests
Using put which is for updates
Using exchange without specifying HTTP method
3fill in blank
hard

Fix the error in the assertion to check if the response status is 200 OK.

Spring Boot
assertEquals(HttpStatus.[1], response.getStatusCode());
Drag options to blanks, or click blank then click option'
ANOT_FOUND
BCREATED
COK
DBAD_REQUEST
Attempts:
3 left
💡 Hint
Common Mistakes
Using NOT_FOUND (404) instead of OK
Using BAD_REQUEST (400)
Using CREATED (201) which is for POST
4fill in blank
hard

Fill both blanks to send a POST request with a JSON body and receive a response as a String.

Spring Boot
ResponseEntity<String> response = restTemplate.[1]("/api/items", [2], String.class);
Drag options to blanks, or click blank then click option'
ApostForEntity
BnewItem
CgetForEntity
DHttpEntity
Attempts:
3 left
💡 Hint
Common Mistakes
Using getForEntity for POST
Trying to use HttpEntity with postForEntity (pass body directly)
Confusing newItem as a method
5fill in blank
hard

Fill all three blanks to create a TestRestTemplate with basic authentication and send a GET request.

Spring Boot
TestRestTemplate restTemplate = new TestRestTemplate().withBasicAuth("[1]", "[2]");
ResponseEntity<String> response = restTemplate.[3]("/api/secure");
Drag options to blanks, or click blank then click option'
Auser
Bpassword
CgetForEntity
DpostForEntity
Attempts:
3 left
💡 Hint
Common Mistakes
Using postForEntity for GET requests
Swapping username and password
Leaving authentication blank