Java Programming - need help on junit..
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
[1014 byte] By [
JUNITa] at [2007-11-26 23:31:09]

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
JUNITa at 2007-7-10 14:42:46 >

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
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 ™. 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?"
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