1
\$\begingroup\$

I am creating unit tests but the class uses objects that have long JSON. What can I do to improve the readability of this?

In tests, only insert those JSON really needed for the test. The class I tested is responsible in turn that JSON in a modified spreadsheet (xlsx) and it seems difficult to maintain the code.

@Test
public void testBuildSheet_validSheet_NoMedia() throws Exception {
    BasicDBObject specPlanilha = (BasicDBObject) JSON.parse("{\"spec_veiculo\" : \"BuscaDescontos\" , \"spec_canal\" : \"Homepage\" , \"spec_formato\" : \"SuperBanner\" , \"spec_linha-criativa\" :  null  , " +
            "\"spec_tamanho\" : \"728x90\" , \"spec_extensao\" : \"jpg/png\" , \"peca_peso\" : \"50kb\" , \"imagem_peso\" : \"50kb\" , " +
            "\"peca_flash\" : \"4 até 10\" , \"peca_clicktag\" : \"on (release) {\\n  getURL(_root.clickTAG, '_blank');\\n}\" , " +
            "\"peca_framerate\" : \"24fps\" , \"peca_tempo\" : \"30s\" , \"spec_observacoes\" :  null  , " +
            "\"peca_nome\" : \"Whirlpool_LinhaBranca_BuscaDescontos_Homepage_SuperBanner_-.jpg\" , \"extras\" : " +
            "{ \"linha\" : \"10\" , \"Rich Media\" : \"Não\" , \"Link Especificação\" : \"http://portal.agenciaclick.com.br/midiaSP/Midia/Lists/Tabela%20de%20Especificao%20e%20Formato%20de%20Peas/DispForm.aspx?ID=3405&Source=http%3A%2F%2Fportal%2Eagenciaclick%2Ecom%2Ebr%2FmidiaSP%2FMidia%2FLists%2FTabela%2520de%2520Especificao%2520e%2520Formato%2520de%2520Peas%2FSites%2Easpx\" ," +
            " \"Data Entrega Mídia\" :  null  , \"Data Disparo/Subida\" :  null }}");
    List<BasicDBObject> listSpec = new ArrayList<>();
    listSpec.add(specPlanilha);
    BasicDBObject specFields = (BasicDBObject) JSON.parse("{\"peca_nome\": \"Whirlpool_LinhaBranca_BuscaDescontos_Homepage_SuperBanner_-.jpg\",\n" +
            "      \"extras\": {\n" +
            "        \"linha\": \"10\",\n" +
            "        \"Rich Media\": \"Não\",\n" +
            "        \"Link Especificação\": \"http://portal.agenciaclick.com.br/midiaSP/Midia/Lists/Tabela%20de%20Especificao%20e%20Formato%20de%20Peas/DispForm.aspx?ID=3405&Source=http%3A%2F%2Fportal%2Eagenciaclick%2Ecom%2Ebr%2FmidiaSP%2FMidia%2FLists%2FTabela%2520de%2520Especificao%2520e%2520Formato%2520de%2520Peas%2FSites%2Easpx\",\n" +
            "        \"Data Entrega Mídia\": null,\n" +
            "        \"Data Disparo/Subida\": null\n" +
            "      }}");

    ValidacaoResultado myValidation = new ValidacaoResultado().setId("123")
            .setResultadoConsolidado("NO_MEDIA")
            .setResultado(new BasicDBObject())
            .setSpec(specFields);
    oknok.validacao.entities.Planilha planilha = new Planilha()
            .setPath(PlanilhaWorkbook.class.getClass().getResource("/planilhaWorkbook/T001.xlsx").getPath())
            .setNomeHash("noMediaTest.xlsx")
            .setSpec(listSpec);

    List<ValidacaoResultado> myList = new ArrayList<>();
    myList.add(myValidation);

    File actual = planilhaWorkbook.buildSheet(myList, planilha);
    SoftAssertions soft = new SoftAssertions();

    soft.assertThat(actual.getName()).isEqualTo(planilha.getNomeHash());
    soft.assertAll();
}
\$\endgroup\$
2
\$\begingroup\$

Ideally, the source data and the expected result of the function under test are both visible inside the test case. That readers can see clearly what is being tested and what the function does.

When the source and result data are relatively big, it's better to not include in the test case, but to keep in files in test resources. This is usually not great, as readers lose visibility of what's really being tested, but if large data pollutes the test case making it unreadable, that's a worse option.

In your case, only the source data (json) is in the test case, the result (xlsx) is outside. In this case, since the JSON is pretty unreadable in the code, I recommend to move it to a resource file, like you did with the xlsx. I think that's the most maintainable option for you.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.