Reputation: 69
I'm bloody beginner what Java tests are about.
I have a class that writes attributes to a (AsciiDoc/ .adoc) file. The attributes are getter (from XMLBeam).
How do I handle this. What do I mock or no mock at all?
I just need an idea how to solve something like this.
That would be great.
public void createFile(Definitions definitionsProjection, String locationToSafe) throws IOException {
FileWriter fileWriter = new FileWriter(locationToSafe + "AsciiDoc.adoc", false);
PrintWriter writer = new PrintWriter(fileWriter);
writer.write("[.text-center]\n= " + definitionsProjection.Title() + "\n");
writer.write("\n|===");
writer.write("\n| Version | Date | Author | Comment\n");
writer.write("\n| " + definitionsProjection.getVersion());
writer.write("\n| " + definitionsProjection.getReleaseDate());
writer.write("\n| " + definitionsProjection.getAuthor() + "| \n");
writer.write("\n|=== \n");
}
My first idea was to mock the XMLBeam getter and write it into the file. Then they read the file and check if everything fits. Is there a prettier/better solution?
Thank you in advance
Upvotes: 0
Views: 414
Reputation: 189
Testing is about recreating the scenario and then expecting something to come out. When you use Asserts you can define your expectation and the actual outcome and compare them. When the expectation and the outcome are equal you get a pass on your test.
I would advise you to look into the documentation of Junit 5 or guides like the one from Baeldung: https://www.baeldung.com/junit-5
Make sure you think about all kinds of possibilities to test: What if the file is not found? What is the file is empty?
Build your test methods on that and assert your preferred answer with the actual outcome and see if you've built solid code.
Upvotes: 1