I have the following controller class in my Spring Boot app.
package com.tsdevelopment.springbootrest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
import java.util.List;
@RestController
public class SpringController {
private List<String> myList = Arrays.asList("Item 1", "Item 2");
@Autowired
RestTemplate restTemplate;
@GetMapping("/v1/resources")
public ResponseEntity<List<String>> getAllResources(@RequestParam(required = false) String parameter){
return ResponseEntity.ok(myList);
}
//API to API call
@GetMapping("/v1/resources/inter-api-call")
public ResponseEntity<List<String>> apiCall(){
List<String> apiResponse = restTemplate.getForEntity("http://localhost:8083/v1/resources", List.class).getBody();
return ResponseEntity.ok(apiResponse);
}
}
And i am trying to test it with the following class:
package com.tsdevelopment.springbootrest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import java.util.Arrays;
import java.util.List;
import org.hamcrest.Matchers;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringControllerTest {
@InjectMocks
SpringController springController;
@Mock
RestTemplate restTemplate;
@LocalServerPort
private int port;
@BeforeEach
public void setUp(){
RestAssured.port = port;
MockitoAnnotations.openMocks(this);
}
@Test
public void apiCall(){
Mockito.when(restTemplate.getForEntity("http://localhost:8083/v1/resources", List.class)).thenReturn(ResponseEntity.ok(Arrays.asList("Item 1", "Item 2")));
System.out.println("RestTemplate response: " + restTemplate.getForEntity("http://localhost:8083/v1/resources", List.class));
System.out.println("Controller response: " + springController.apiCall());
RestAssured.given()
.when()
.get("/v1/resources/inter-api-call")
.then()
.statusCode(200)
.body("", Matchers.equalTo("[\"Item 1\",\"Item 2\"]"));
}
}
When i execute the test it fails. The logs i get are:
RestTemplate response: <200 OK OK,[Item 1, Item 2],[]> Controller response: <200 OK OK,[Item 1, Item 2],[]>
And the test error log is:
java.lang.AssertionError: 1 expectation failed. Expected status code <200> but was <500>.
Instead of using @Mock on RestTemplate, you should use @MockBean, which ensures that Spring Boot uses your mock in the context.
When you use @Mock, you are creating a mock instance of the object that only exists within the test class itself. This mock is not visible to Spring's dependency injection mechanism, so spring is still using the real RestTemplate bean from its context during the actual test execution.
Also remove the @InjectMocks annotation because it's unnecessary since you're relying on Spring's dependency injection with @Autowired.
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringControllerTest {
@Autowired
SpringController springController;
@MockBean
RestTemplate restTemplate;
@LocalServerPort
private int port;
@BeforeEach
public void setUp(){
RestAssured.port = port;
}
@Test
public void apiCall(){
Mockito.when(restTemplate.getForEntity("http://localhost:8083/v1/resources", List.class))
.thenReturn(ResponseEntity.ok(Arrays.asList("Item 1", "Item 2")));
RestAssured.given()
.when()
.get("/v1/resources/inter-api-call")
.then()
.statusCode(200)
.body("", Matchers.equalTo("[\"Item 1\",\"Item 2\"]"));
}
}