Encoding problem in Spring Boot MockMvc tests

I’m implementing integration tests for my Spring Boot application using MockMvc. The problem is that I need to use russian characters in my request/response bodies and they keep changing to unreadable symbols

Here’s my config:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = InfoApplicationConfig.class)
@TestPropertySource(properties = {"spring.config.location=classpath:application-it.yml"})
@AutoConfigureMockMvc
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class TerminalControllerIT {

    @Autowired
    MockMvc mockMvc;

    @LocalServerPort
    private int port;

    private String baseUrl = "http://localhost";

    private final String validTerminalId = "eee00000-0000-0000-0000-000000000000";

    private final String invalidTerminalId = "abc00000-0000-0000-0000-000000123456";

    private NewTerminalDto testSubject;

    @Autowired
    private static ObjectMapper objectMapper;

    @Autowired
    private TerminalRepository terminalRepository;

    @BeforeEach
    public void setUp() {
        testSubject = NewTerminalDto.builder().country("Россия").region("Москва").city("Москва")
                .street("Ленина").buildingNumber("12A")
                .roomNumber("101").terminalCoordinate("55.7558,37.6176")
                .postCode("123456").terminalNumber("123")
                .isClosed(false).cashDepositWithdrawal(false).moneyTransfer(true).payment(true).nfc(true)
                .banknotesPerPack(100)
                .biometrics(false).encashmentService(true).cashDeposit(true)
                .openingTime("08:00")
                .closingTime("18:00")
                .build();
        baseUrl = baseUrl.concat(":" + port).concat("/api/v1/info-service/terminals");
    }

Test 1:

@Test
    @DisplayName("Should return terminal by id")
    void getTerminalInfoByIdPositiveTest() throws Exception {
        MvcResult result = mockMvc.perform(get(baseUrl + "/" + validTerminalId)
                        .characterEncoding("UTF-8")
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andReturn();

        String jsonResponse = result.getResponse().getContentAsString();
        TerminalGetResponse actualResponse = objectMapper.readValue(jsonResponse, TerminalGetResponse.class);

        assertNotNull(actualResponse);
        assertNotNull(actualResponse.getTerminalNumber());
        assertNotNull(actualResponse.getRegion());
        assertEquals("ТРМ-001", actualResponse.getTerminalNumber());
        assertEquals("Москва", actualResponse.getRegion());
    }

Response:

Expected :ТРМ-001
Actual   :ТРÐ-001

Test 2:

 @Test
    @DisplayName("Adding branch")
    void addBranch() throws Exception {
        BranchPayload body = BranchStubs.createBranchPayload(0);

        mockMvc.perform(post(baseUrl)
                        .content(objectMapper.writeValueAsBytes(body))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                        .characterEncoding("UTF-8"))
                .andExpect(status().isOk());

        BankBranchesModel bankBranchesModel = BankBranchesModel.builder().branchNumber(body.getBranchNumber()).build();

        assertNotNull(bankBranchesRepository.findOne(Example.of(bankBranchesModel)).orElse(null));
    }

Result:

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /api/v1/info-service/branches
       Parameters = {}
          Headers = [Content-Type:"application/json;charset=UTF-8", Content-Length:"907"]
             Body = {"branchNumber":"8500","country":"??????","region":"?????????? ???????","city":"????????","street":"???????????","buildingNumber":"5","postCode":"117997","branchCoordinate":"55.696225, 37.544539","ramp":true,"phoneNumber":"74955555550","isClosed":false,"openingTime":"00:00","closingTime":"23:55","dayOfWeek":["???????????","???????","?????","???????","???????"],"currencyExchange":true,"foreignCurrency":true,"moneyTransfer":true,"cashWithdrawal":true,"payment":true,"replenishCard":true,"replenishAccount":true,"hasDeposit":true,"hasCredit":true,"consultation":true,"insurance":true,"bik":"044525220","kpp":"773643002","inn":"7707083890","paymentAccount":"40702810562000000000","correspondentAccount":"30101810400000000225","bankNameFull":"?? «FinTech Bank»","okpo":"09610477","ogrn":"1027700057410","swift":"LIBBRUMM007"}
    Session Attrs = {}

MockHttpServletResponse:
           Status = 400
    Error message = null
          Headers = [Content-Type:"application/json"]
     Content type = application/json
             Body = {"bankNameFull":"must match "^(?!\s*$)[0-9A-Za-z?-??-? !"#$%&'()*+,\-./:;<=>?@\[\\\]^_{|}~]{1,30}$""}
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

Tried to set encoding in test class properties / for objectMapper / for mockMvc – none of that worked for me
Also tried doing it in my migration files which actually doesn;t make sense because characters in request are already messed up

New contributor

D Bystrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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