0

Is there any way to unit-test these methods, since they are not taking any parameters and dont return anything i dont know how i

import java.io.*; public class readnWriteFile { String[] names = {"me", "you", "him", "her"}; public void writeFiles(){ BufferedWriter buff; try { buff = new BufferedWriter(new FileWriter("justsomefile.txt")); buff.write("write to this file"); for (String name : names) { buff.write("\n" + name); } buff.close(); } catch (IOException e) { throw new RuntimeException(e); } } public void readFiles(){ try { BufferedReader reader = new BufferedReader(new FileReader("justsomefile.txt")); String line; while((line = reader.readLine()) != null){ System.out.println(line); } reader.close(); } catch (IOException e) { throw new RuntimeException(e); } } }

28th Dec 2022, 11:13 PM
Lenoname
1 Réponse
+ 4
First of all you are cheating, because your "readFiles" method is actually writing to a file as well. So your method name is deceiving. 🙂 If you can decide what you want to test, for example what happens if the file doesn't exist, or the content was really written to the file, you can find tools in popular testing frameworks such s JUnit to help with this. https://www.javacodegeeks.com/2015/01/testing-with-files-and-directories-in-junit-with-rule.html
29th Dec 2022, 6:33 AM
Tibor Santa
Tibor Santa - avatar