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]
# 1
what do you need to test about it? that it runs without an exception? what?
georgemca at 2007-7-10 14:42:46 > top of Java-index,Java Essentials,Java Programming...
# 2
thanks for replyingi need to test if the method is working. thanks,john
JUNITa at 2007-7-10 14:42:46 > top of Java-index,Java Essentials,Java Programming...
# 3

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

georgemca at 2007-7-10 14:42:46 > top of Java-index,Java Essentials,Java Programming...
# 4

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 > top of Java-index,Java Essentials,Java Programming...
# 5

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

georgemca at 2007-7-10 14:42:46 > top of Java-index,Java Essentials,Java Programming...
# 6
alright. i will give it a try. thanks for the advice and time.
JUNITa at 2007-7-10 14:42:46 > top of Java-index,Java Essentials,Java Programming...
# 7

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 [url=http://en.wikipedia.org/wiki/Test-driven_development]test first[/url], and your code will be so much cleaner and easier to debug

georgemca at 2007-7-10 14:42:46 > top of Java-index,Java Essentials,Java Programming...
# 8
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?}
JUNITa at 2007-7-10 14:42:46 > top of Java-index,Java Essentials,Java Programming...
# 9

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

georgemca at 2007-7-10 14:42:46 > top of Java-index,Java Essentials,Java Programming...
# 10
thanks again... i guess i have to re structure again..:)
JUNITa at 2007-7-10 14:42:46 > top of Java-index,Java Essentials,Java Programming...
# 11

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

georgemca at 2007-7-10 14:42:46 > top of Java-index,Java Essentials,Java Programming...
# 12
thanks.. i will take note. reading TDD now...
JUNITa at 2007-7-10 14:42:46 > top of Java-index,Java Essentials,Java Programming...