package com.example.mqspring; import static org.junit.jupiter.api.Assertions.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import com.fasterxml.jackson.databind.ObjectMapper; import com.pgf.mqspring.model.SampleRequestModel; import com.pgf.mqspring.model.SampleResponseModel; @SpringBootTest class MqSpringApplicationTests { @Test void contextLoads() { } @Autowired private MockMvc mockMvc; @Test public void test1() throws Exception { String id = "1"; String msg = "HelloWorld!!!"; List> mqcih = new ArrayList>(); HashMap sampleMap = new HashMap(); sampleMap.put("Function", "MQCFUNC_NONE"); sampleMap.put("Facility", "BRFA"); sampleMap.put("TransactionId", "EK12"); sampleMap.put("NextTransactionCode", ""); sampleMap.put("ReceiveMapVector", "16,1802,'I'"); mqcih.add(sampleMap); SampleRequestModel request = new SampleRequestModel(id, msg, mqcih); ObjectMapper objectMapper = new ObjectMapper(); MvcResult result = mockMvc.perform( post("/createSample") .content(objectMapper.writeValueAsString(request)) // リクエストボディを指定 .contentType(MediaType.APPLICATION_JSON) // Content Typeを指定 ).andExpect(status().isCreated()).andReturn(); // ).andExpect(status().isOk()).andReturn(); SampleResponseModel sampleResponce = objectMapper.readValue(result.getResponse().getContentAsString(), SampleResponseModel.class); assertEquals(id, sampleResponce.getId()); assertEquals(msg, sampleResponce.getMsg()); } }