After a long break, recently I came across junit testing. This was when I was helping my team member in writing junit tests and suggested where to start from. Then, there was an issue in running a TestSuite.
The code that I used before is something like below:
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for something");
suite.addTestSuite(SomeClass.class);
return suite;
}
}
But, when i tried to run, I got below error:
java.lang.Exception: No runnable methods
By the way, this is with junit-4. So, here is what we figured out -
In junit-4, they use annotations for TestSuite. So, the old syntax is not working. Here is working syntax
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses( { SomeClass.class})
public class AllTests {
}
So simple. Isn't it?
Also, download junit coverage plugin for eclipse using this update site http://update.eclemma.org/ . More details are here regarding eclemma
Thursday, November 26, 2009
Junit testing
Wednesday, November 18, 2009
Handling "Ctrl+C" in Java
It is very simple. One just need to use addShutdownHook method and pass a thread where you can add code to do what ever you wanted to.
Sample code is here -
public class ShutdownHookDemo {
public static void main(String[] args)
{
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run()
{
System.out.println("Ctrl+C was pressed by some one and I am exiting now");
}
});
int i = 0;
while(true)
{
System.out.println("I value:" + i++);
System.out.println("Going to sleep now for 2 secs");
try{
Thread.currentThread().sleep(2000);
}
catch(InterruptedException ie)
{
System.out.println("Someone woke me up:("+ ie);
ie.printStackTrace();
}
}
}
}
So simple it is.. isn't it? and I just realized that hadoop used it to close files opened when some one press "ctrl+C". Just one sentence to describe it "Simple yet powerful."
Friday, September 18, 2009
Confused in Java
I had a code where I was using writeBytes() method of DataOutput for writing a string to file. And, it created some issues while reading. Then, I replaced the same with write(str.getBytes()). This went fine without causing any issue. Confused now on what is the difference between these two methods. Still exploring...
Tuesday, November 20, 2007
using tab as field separator while using sort command in bash shell
It was some what strange to me when I experienced that tab as field separator is not supported (not exactly supported. but didn't make it easy to use) in sort command in bash shell. Probably it depends on sort version. But, some how it didn't work in my machine.
The error that I got when I included tab as field separator is:
sort: multi-character tab '\t'
So, after doing search over the above error,here is one of the solutions that worked for me:
TAB=$(printf "\t")
and then using sort -t "$TAB".
For ex:
sort -t "$TAB" -k2,2 -n entries.txt
Friday, October 26, 2007
Gmail limits forward filters
I had created some filters in my gmail which forwards mails to different accounts internally. Now, today, while I was creating a filter to forward it to some specific account, I got the below error
"Could not create a filter. Too many forward addresses" :)
The limit that google allowing is only 21. I am wondering about this magic number? why 21??