This discussion is archived
12 Replies Latest reply: Mar 30, 2007 9:07 AM by 807606 RSS

need help on junit..

807606 Newbie
Currently Being Moderated
hi guys,
i need help on writing a junit test for this method. can anyone help me ?

public void processFile(ArrayList<File> fileList){
System.out.println("Loading please wait.....");
try{
for (File i: fileList){
System.out.println(i);
System.out.println("..........");
Scanner srRead = new Scanner(i);
while (srRead.hasNextLine()){
Scanner sr = new Scanner(srRead.nextLine()).useDelimiter("#");
Vector<String> vectorList = new Vector<String>();
while (sr.hasNext()){
vectorList.add(sr.next());
}
setEventIDFreq(vectorList);
setIFNFreq(vectorList);
}
}
checkProcess(avgList);
calAvg(mapAvgFreq,mapTotalAvg);
}
catch (ArrayIndexOutOfBoundsException arrayOut){
System.out.println("Cant read this .log file...");
}
catch (FileNotFoundException notfound){
System.out.println("File not found...");
}

}

thanks,
john
  • 1. Re: need help on junit..
    807606 Newbie
    Currently Being Moderated
    what do you need to test about it? that it runs without an exception? what?
  • 2. Re: need help on junit..
    807606 Newbie
    Currently Being Moderated
    thanks for replying
    i need to test if the method is working.
    thanks,
    john
  • 3. Re: need help on junit..
    807606 Newbie
    Currently Being Moderated
    thanks for replying
    i need to test if the method is working.
    thanks,
    john
    well, obviously you want to test if it's working! I was looking for something more specific. as it is, it doesn't look too testable. anything that goes wrong seems to just get caught and send to the console
  • 4. Re: need help on junit..
    807606 Newbie
    Currently Being Moderated
    opps sorry,
    setEventIDFreq(vectorList);
    setIFNFreq(vectorList);
    checkProcess(avgList);
    calAvg(mapAvgFreq,mapTotalAvg);

    processFile method contains the above private method which they process the data and put into a treemap collection. what i need to know from with processFile method is, does the treemap contain the data that i want?

    thanks for helping
  • 5. Re: need help on junit..
    807606 Newbie
    Currently Being Moderated
    opps sorry,
    setEventIDFreq(vectorList);
    setIFNFreq(vectorList);
    checkProcess(avgList);
    calAvg(mapAvgFreq,mapTotalAvg);

    processFile method contains the above private method
    which they process the data and put into a treemap
    collection. what i need to know from with processFile
    method is, does the treemap contain the data that i
    want?

    thanks for helping
    and you know up-front what the data should be, right? simply examine the TreeMap, then, and compare what you think should be there with what is actually there. the essence of basic unit testing
  • 6. Re: need help on junit..
    807606 Newbie
    Currently Being Moderated
    alright. i will give it a try. thanks for the advice and time.
  • 7. Re: need help on junit..
    807606 Newbie
    Currently Being Moderated
    alright. i will give it a try. thanks for the advice
    and time.
    no problem. to be honest, I hardly ever write a test to test an existing class or method. write the test first, and your code will be so much cleaner and easier to debug
  • 8. Re: need help on junit..
    807606 Newbie
    Currently Being Moderated
    hahaha.. me too... its a sch assignment... argh... and i never use junit before... its killing me...

    public void testProcessFile() {     
    sl.processFile(sl.readDir("C:/LogData/OneLog"));
    //what should i write here??
    }
  • 9. Re: need help on junit..
    807606 Newbie
    Currently Being Moderated
    hahaha.. me too... its a sch assignment... argh...
    and i never use junit before... its killing me...

    public void testProcessFile() {     
    sl.processFile(sl.readDir("C:/LogData/OneLog"));
    //what should i write here??
    }
    nothing. start again. I'd decouple the method from working on files, to be honest. the method is doing too many different things, which is A Bad Thing &#8482;. for starters, that makes it harder to reuse, and also to test. throw the processFile method away. you want a nice processData method that doesn't depend on a File to do its work. a test that relies on a File being present is an awkward test. start by asking "what should this method achieve?"
  • 10. Re: need help on junit..
    807606 Newbie
    Currently Being Moderated
    thanks again... i guess i have to re structure again..:)
  • 11. Re: need help on junit..
    807606 Newbie
    Currently Being Moderated
    thanks again... i guess i have to re structure
    again..:)
    like I said, start with the test. seriously. don't even write the beginning of your class first, start with the test. and don't pass Files into the method that does the work, extract the data somewhere else and pass that into the method. that way, you don't need to muck around creating Files just for testing, and the test suddenly becomes a lot simpler to write. do some research into TDD, though. it'll change the way you write code
  • 12. Re: need help on junit..
    807606 Newbie
    Currently Being Moderated
    thanks.. i will take note. reading TDD now...