mercredi 23 mai 2018

JUNIT - Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token

I want to test my addSomething method, I've created setUp method that look like this:

@Before
public void setUp() throws Exception {
    activitiesService = new ActivitiesService(databaseControllerMock);
    when(databaseControllerMock.get(anyString(), any())).thenReturn(item);
    when(item.getJSON(anyString())).thenReturn("test");
}

In this case I have this error message:

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'test': was expecting 'null', 'true', 'false' or NaN

So I changed:

when(item.getJSON(anyString())).thenReturn("true");

In this case I'm getting this kind of error:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.ArrayList out of VALUE_TRUE token at [Source: (String)"true"; line: 1, column: 1]

All of these is about of this part of my method:

try {
        Item item = dbService.get(tableName, Collections.singletonList(primaryKey));
        String measurementsJSON = item.getJSON("measurements");
        String mealsJSON = item.getJSON("meals");
        String trainingsJSON = item.getJSON("trainings");

        ObjectMapper objectMapper = new ObjectMapper();

        List<Measurement> measurements = objectMapper.readValue(measurementsJSON, new TypeReference<List<Measurement>>() {
        });
        List<Meal> meals = objectMapper.readValue(mealsJSON, new TypeReference<List<Meal>>() {
        });
        List<Training> trainings = objectMapper.readValue(trainingsJSON, new TypeReference<List<Training>>() {
        });

DbService and Item are Mocks. Can someone help me with this problem?

Aucun commentaire:

Enregistrer un commentaire