Happy Chinese new year

 

Advertisements

Big suprise in Date

Try this code and you will know.

import java.util.Date;

public class Test {
  public static void main(String[] args) {
    Date date = new Date();
    for (int i = 1; i < 13; i++) {
      date.setMonth(i);
      System.out.println(i + " | " + date.getMonth());
    }
  }
}

Digg This


Auto test in Eclipse with Ant

Eclipse has bundled with both JUnit and Ant, it would be great if we take the advantage to automatically run unit test every time the code changes. Here is how:

1. Make sure the ANT_HOME system variable is defined with the correct path.

2. Make sure the ant_junit.jar is included in the project class path.

3. Create a build.xml, which defined the ant task, like below:

<project name="auto-test" default="test">
    <property name="tst-dir" location="The path of your test" />
    <path id="classpath.base" />
    <path id="classpath.test">
        <pathelement location="your junit.jar path" />
        <pathelement location="${tst-dir}" />
        <path refid="classpath.base" />
    </path>
    <target name="test" description="Run the tests">
        <junit>
            <classpath refid="classpath.test" />
            <formatter type="brief" usefile="false" />
            <test name="your test suite name" />
        </junit>
    </target>
</project>
 

 

4. Create a ant typed custom builder.

untitled

5. Specified the build.xml.

untitled2 

6. Set the builder target to “Auto Build”.

You should be able to run your tests when you strike Ctr-S 🙂

 

 

Follow up:

The prevent path problem when the code is running on different environments, we need to change the working directory to project path:

custom_buider

Updated:

If you see this error:
BUILD FAILED
E:STAF_workspacesuadetaskbuild.xml:14: The <classpath> for <junit> must include junit.jar if not in Ant’s own classpath

Make sure you have add the path of junit.jar into the classpath, like below:

    <path id="classpath.test">
        <pathelement location="D:eclipsepluginsorg.junit4_4.3.1junit.jar" />
        <pathelement location="C:UsersthinkTWTopCoderlibdom4j-1.6.1.jar"/>
        <pathelement location="C:UsersthinkTWTopCoderlibjaxen-1.1.1.jar"/>
        <pathelement location="C:UsersthinkTWTopCoderlibmockito-all-1.8.2.jar"/>
        <pathelement location="${tst-dir}" />
        <path refid="classpath.base" />
    </path>