Wednesday, October 28, 2009

JUnit4 public setup method in Eclipse

I converted a JUnit test case from JUnit3 to JUnit4 style. This mostly involves not extending TestCase and using annotations such as @Test and @Before etc. JUnit4 in 60 Seconds is an excellent tutorial.

I ran into a problem, however. A test method did not run after the conversion. Eclipse did not throw any error. Its JUnit View simply said "finished in 0 seconds", like it did not run it at all. Eventually I found that the setUp() method was still "protected" as in JUnit3. Everything worked after I changed it to "public".

    /**
* Setup.
*/
@Before
public void setUp() {
// ...
}



Nowhere said that the setup method must be public. It might be an Eclipse thing. Anyway, I just want to note it down here and will revisit it later.

Tuesday, October 27, 2009

Run service command on Linux

If cruisecontrol has been configured as a service on Linux, you can check status/start/stop it with the service command. For example,

service cruisecontrol status

To work withe other services, just replace "cruisecontrol" with other service names.

Checkstyle 5 and maven-checkstyle-plugin 2.3

I installed Eclipse Checkstyle plugin, which uses Checksytle 5. The latest maven-checkstyle-plugin, version 2.3, uses Checkstyle 4.4.

Everything works fine in Eclipse with Checkstyl5 5. Things got tricky when I ran "mvn checkstyle:checkstyle". If I didn't specify custom configuration, it uses the default template from Checkstyle 4.4. So the report will have a lot of false alarm because it did not recognize Java 5 checks. So I specified a custom configuration like this.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.3</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
</configuration>
</plugin>

The template, checkstyle.xml, is the default "Sun Checks" from Checkstyle 5.0. However, maven threw an error "Unable to instantiate JavadocPackageCheck". According to this post, it is because "You are trying to use a Checkstyle check that was introduced in Checkstyle 5.0. The Checkstyle Plugin currently uses Checkstyle 4.4."

It looks like we have to live with this discrepancy until maven-checkstyle-plugin 2.4, which is based on Checkstyle 5, is released.

Friday, October 16, 2009

Specify initial size for Commons DBCP BasicDatasource

It is fairly easy to setup database pooling with Spring and Commons DBCP BasicDatasource. However, the pool's default initial size is 0 if you look it up in the source code. To change that, specify "initialSize" in your configuration.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="initialSize">
<value>10</value>
</property>
</bean>

Thursday, October 8, 2009

Get rid of "No runnable methods"

I have a couple of base tests. They are simply for setting up and extension by sub test classes, and have no test methods. Their names are the pattern of "*Tests". This works fine with maven "mvn test". But in Eclipse, when you right click "src/test/java" and "Run as > JUnit test", Eclipse will throw errors "No runnable methods". The trick is to add @Ignore on these base test classes so Eclipse won't bother again. This is of course JUnit 4 style annotation.