如何在jsonPath中测试JSON数组

时间:2019-10-16 14:53:21

标签: json junit

我想测试以下响应:

{projetos=[{"id":null,"valorEconomico":null,"servicosPrestados":null,"serviceType":"mock","autenticado":null,"dtFim":"2019-10-16T14:25:49.498+0000","dtInicio":"2019-10-16T14:25:49.498+0000","tecnologias":"mock","dsProjeto":"projeto1","un":null,"nrHoras":null,"setor":null,"statusProjetoDescricao":null,"gerenteProjetoEmail":null,"gerenteProjetoNome":null,"nomeCliente":null,"cometarios":null}],
servicesTypes=["MOCK"]}

我正在尝试使用此方法:

    private void checkAtributesIProjeto(ResultActions resultActions, Boolean isArray) throws Exception {
    String jsonIndex = isArray ? "$[0]." : "$.";
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000");
    df.setTimeZone(TimeZone.getTimeZone("GMT+0000"));

    resultActions.andExpect(jsonPath(jsonIndex + "dsProjeto", is(projeto.getDsProjeto())))
            .andExpect(jsonPath(jsonIndex + "setor", is(projeto.getSetor())))
            .andExpect(jsonPath(jsonIndex + "un", is(projeto.getUn())))
            .andExpect(jsonPath(jsonIndex + "dtInicio", is(df.format(projeto.getDtInicio()))))
            .andExpect(jsonPath(jsonIndex + "dtFim", is(df.format(projeto.getDtFim()))))
            .andExpect(jsonPath(jsonIndex + "valorEconomico").value(projeto.getValorEconomico()))
            .andExpect(jsonPath(jsonIndex + "nrHoras", is(projeto.getNrHoras())))
            .andExpect(jsonPath(jsonIndex + "autenticado", is(projeto.getAutenticado())))
            .andExpect(jsonPath(jsonIndex + "servicosPrestados", is(projeto.getServicosPrestados())))
            .andExpect(jsonPath(jsonIndex + "tecnologias", is(projeto.getTecnologias())))
            .andExpect(jsonPath(jsonIndex + "serviceType", is(projeto.getServiceType())))
            .andExpect(jsonPath("$.servicesTypes", hasSize(1)))
            .andExpect(jsonPath("$.servicesTypes", hasItem("MOCK")));

}

但是发生以下错误:

 java.lang.AssertionError: No value at JSON path "$.servicesTypes"

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

关于JSONAssert:

我为您写了一个小例子:

package com.answers;    

import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;    

public class JsonTest {    

    @Test
    public void testJsonString() throws JSONException {
        String actual = "{projetos=[{\"id\":null,\"valorEconomico\":null,\"servicosPrestados\":null,\"serviceType\":"
                + "\"mock\",\"autenticado\":null,\"dtFim\":\"2019-10-16T14:25:49.498+0000\",\"dtInicio\":"
                + "\"2019-10-16T14:25:49.498+0000\",\"tecnologias\":\"mock\",\"dsProjeto\":\"projeto1\","
                + "\"un\":null,\"nrHoras\":null,\"setor\":null,\"statusProjetoDescricao\":null,"
                + "\"gerenteProjetoEmail\":null,\"gerenteProjetoNome\":null,\"nomeCliente\":null,"
                + "\"cometarios\":null}],servicesTypes=[\"MOCK\"]}";

        //This test is OK
        JSONAssert.assertEquals("{projetos=[{\"id\":null,\"valorEconomico\":null,\"servicosPrestados\":null,"
                + "\"serviceType\":\"mock\",\"autenticado\":null,\"dtFim\":\"2019-10-16T14:25:49.498+0000\","
                + "\"dtInicio\":\"2019-10-16T14:25:49.498+0000\",\"tecnologias\":\"mock\",\"dsProjeto\":"
                + "\"projeto1\",\"un\":null,\"nrHoras\":null,\"setor\":null,\"statusProjetoDescricao\":null,"
                + "\"gerenteProjetoEmail\":null,\"gerenteProjetoNome\":null,\"nomeCliente\":null,\"cometarios"
                + "\":null}],servicesTypes=[\"MOCK\"]}", actual, true);

        //This test will fail
        JSONAssert.assertEquals("{projetos=[{\"id\":otro_valor,\"nomeCliente\":null,\"cometarios"
                + "\":null}],servicesTypes=[\"MOCK\"]}", actual, true);
    }    

}

罐子:

<!-- https://mvnrepository.com/artifact/org.skyscreamer/jsonassert -->
<dependency>
    <groupId>org.skyscreamer</groupId>
    <artifactId>jsonassert</artifactId>
    <version>1.5.0</version>
    <scope>test</scope>
</dependency>
相关问题