Mock RestTemplate call rreturns HTTP 500

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"]"));
}
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật