mercredi 13 juin 2018

MockMvc response body has random numbers

I'm testing a get request using MockMvc. Weirdly it returns random numbers when I make assertions. Only string values are correct.

This is my test class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
@SpringBootTest
public class MyControllerTest {

@Autowired
private WebApplicationContext context;
private MockMvc mvc;

@Before
public void setUp() {
    mvc = MockMvcBuilders.webAppContextSetup(context).build();
}

@Test
public void testGetRequest() throws Exception {
    String json = "[{\"id\":1,\"name\":\"foobar\"]";

    mvc.perform(get("/my/interface")
            .param("name", "foobar")
            .content(json))
            .andExpect(status().isOk())
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(jsonPath("$[0].name").exists())
            .andExpect(jsonPath("$[0].name").value("foobar"))
            .andExpect(jsonPath("$[0].id").exists())
            .andExpect(jsonPath("$[0].id").value(1));
}
}

And this is the DTO that is being returned by the controller:

@JsonInclude(Include.NON_NULL)
public class MyDto {

private String name;

private Byte id;

//getters and setters
}

My expected value is 1, but in actual it is 1569. This seems really strange to me, is it possible that the data type Byte is causing the problem?

Aucun commentaire:

Enregistrer un commentaire