JUnit AssertionFailedError: Text in the file differs in java -


i using junit test testing purposes, facing problem of assertionfailederror. using command line arguments pass test cases main class.

below main.java code

public class main {  public static void main(string[] args) throws ioexception {       //storing commands, words, files     arraylist<string> commands = new arraylist<>();     arraylist<string> words = new arraylist<>();     arraylist<string> files = new arraylist<>();      for(string arg: args){         if(arg.contains("."))             files.add(arg);         else if(arg.contains("-") && !arg.contains("--"))             commands.add(arg);         else{             if(!arg.contains("--"))             words.add(arg);         }     }      for(string file : files ){          file originalfile = new file(file);          //check if textfile exists         if(originalfile.exists()){             if(words.size() == 2){                 string = words.get(0), to=words.get(1);                 bufferedwriter bw;                 //if file exists check command                 for(string command : commands){                   if(command.trim().contains("-f")){                         file temp = new file("temp.txt");                         temp.createnewfile();                         //if temp.exists                         if(temp.exists()){                             bw = new bufferedwriter(new filewriter(temp));                              //fetch lines orginal file                             list<string> lines = files.readalllines(paths.get(originalfile.getname()));                               //add treemap                             treemap<integer,string> tm = new treemap<>();                              for(int i=0;i<lines.size();i++){                                 tm.put(i,lines.get(i));                             }                              //to check first occurence of word in hashmap                             for(int i=0;i<tm.size();i++){                                 string lastline = tm.get(i);                                 tm.remove(i);                                 tm.put(i,lastline.replacefirst(from,to));                                 if(!lastline.equals(tm.get(i)))                                     break;                             }                              //write treemap text file                             for(string line: tm.values())                                 bw.write(line.trim() + "\n");                             system.out.println("first occurence " + originalfile.getname()+ " changed");                             bw.close();                             originalfile.delete();                             temp.renameto(originalfile);                         }else                             system.out.println("error in creating temp.txt file");                     }                  }               } 

everything working fine, file created. dont think there error in code. below maintest.java

public class maintest {  // utilities  private file createinputfile1() throws exception {     file file1 =  new file("test.txt");     try (filewriter filewriter = new filewriter(file1)) {         filewriter.write("dog animal");     }     return file1; }  private string getfilecontent(string filename) {     string content = null;     try {         content = new string(files.readallbytes(paths.get(filename)));     } catch (ioexception e) {         e.printstacktrace();     }     return content; }  // actual test cases @test public void maintest2() throws exception {     file inputfile1 = createinputfile1();      string args[] = {"-f", "dog", "cat", "--", "test.txt"};     main.main(args);      string expected1 = "cat animal".trim();      string actual1 = getfilecontent("test.txt");      assertequals("the files differ!", expected1, actual1);     asserttrue(files.exists(paths.get(inputfile1.getpath() + ".bck"))); }  } 

everything works fine, file test.txt created , has text in it. facing error of assertionfailederror: the files differ! expected: cat animal[] was: cat animal[ ]

why differ [] , [ ] ?

try this, shall :

string expected1 = "cat animal".trim();  string actual1 = getfilecontent("test.txt").trim();  assertequals("the files differ!", expected1, actual1); 

since trim

 * @return  string value string, leading , trailing white  *          space removed, or string if has no leading or  *          trailing white space. 

hence see diff of space [ ] , [] solution , resolved using trim in both.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -