0

I am working in AEM trying to get create txt files with JSON output so that I can load them into my unit test as strings and test my model / model processors. So far I have this...

public String readFile(String path, Charset encoding) throws IOException
{
    byte[] encoded = Files.readAllBytes(Paths.get(path));
    return new String(encoded, encoding);
}

private String sampleInput = readFile("/test/resources/map/sample-
input.txt",Charset.forName("UTF-8"));

I need sampleInput to take the json that is in 'sampleInput.txt' and convert it to a string. I am also running into issues with the Charset encoding.

Junior Dev
  • 1
  • 1
  • 1
  • 1
  • have a look at [this](https://stackoverflow.com/questions/22914808/how-to-convert-a-json-data-to-string-in-java) – user1207289 Nov 30 '17 at 04:30
  • What testing framework are you using? Add the files (.json) as resources to your solution and have the test load the files. – Jocke Nov 30 '17 at 08:49
  • Yes, I am taking the json string, doing Gson gson = new Gson(); and then adding them to the model – Junior Dev Nov 30 '17 at 14:30

2 Answers2

2

I think the easiest way to manage JSON documents you use for unit testing is by keeping them organized in the classpath. Guava provides a neat wrapper for loading classpath resources.

import com.google.common.base.Charsets;
import com.google.common.io.Resources;

import java.io.IOException;
import java.net.URL;


public class TestJsonDocumentLoader {

    public TestJsonDocumentLoader(Class clazz) {
        this.clazz = clazz;
    }

    public String loadTestJson(String fileName) {
        URL url = Resources.getResource(clazz, fileName);
        try {
            String data = Resources.toString(url, Charsets.UTF_8);
            return data;
        } catch (IOException e) {
            throw new RuntimeException("Couldn't load a JSON file.", e);
        }
    }
}

This can then be used to load arbitrary JSON files placed in the same package as the test class. It is assumed that the files are UTF-8 encoded. I suggest keeping all sources encoded that way, regardless of the OS your team is using. It saves you a lot of trouble with version control.

Let's say you have MyTest in src/test/java/com/example/mytestsuite, then you could place a file data.json in src/test/resources/com/example/mytestsuite and load id by calling

TestJsonDocumentLoader loader = new TestJsonDocumentLoader(MyTest.class);
String jsonData = loader.loadTestJson("data.json");
String someOtherExample = loader.loadTestJson("other.json");

Actually, this could be used for all sorts of text files.

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
  • Great this seems to be working. When i debug the test at this line TestJsonDocumentLoader loader = new TestJsonDocumentLoader(MapModelTest.class); I get 'class has no classes.' Do you know what i could be doing incorrectly here? MapModelTest is the name of my unitTest – Junior Dev Nov 30 '17 at 17:16
  • Not off the top of my head. I'd have to see the full stack trace and the structure of `MapModelTest.java` – toniedzwiedz Nov 30 '17 at 18:03
2

You could have also used object mapper from Jackson as an alternative

public class JsonResourceObjectMapper<T> {
    private Class<T> model;

    public JsonResourceObjectMapper(Class<T> model) {
        this.model = model;
    }

    public T loadTestJson(String fileName) throws IOException{
        ClassLoader classLoader = this.getClass().getClassLoader();
        InputStream inputStream= classLoader.getResourceAsStream(fileName);
        return new ObjectMapper().readValue(inputStream, this.model);
    }
}

And then setup a fixture in the test passing a .class

private JsonClass json;

@Before
public void setUp() throws IOException {
    JsonResourceObjectMapper mapper = new JsonResourceObjectMapper(JsonClass.class);
    json = (JsonClass) mapper.loadTestJson("json/testJson.json");
} 

Note that the testJson.json file is in resources/json folder same as what @toniedzwiedz mentioned

So then you could use the json model as:

@Test
public void testJsonNameProperty(){
    //act
    String name = json.getName();

    // assert
    assertEquals("testName", name);
}
harlandgomez
  • 490
  • 4
  • 15