Brian’s Stuff

Stuff I don’t want to forget (again!)

Using P6Spy to debug DbUnit Maven Plugin

In my last post I described the motivation for switching to from JCL to SLF4J. I had been debugging some unit tests but was not seeing anything in my Log4J. I eventually realised that the problem was because Hibernate had switched from JCL to SLF4J and a logging provider needed to configured for the project.

Unfortunately, fixing the logging issues did not magically fix the bugs in my code and/or test cases. It looked like my test case set-up was not working as expected.

I was using the DbUnit Maven Plugin to load the data set to initialise the test cases. In the end it turned out I had a formatting error in the XML data set file. I had been too lazy to define a schema corresponding to my database tables and now was paying the price. The file looked good and passed basic XML validation.

To solve the problem I decided I needed to see what SQL statements were being executed by the plugin. I used P6Spy to proxy the my JDBC driver.

The plugin is configured via the Maven 2 POM file as illustrated in the example below.

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>dbunit-maven-plugin</artifactId>
 <version>1.0-beta-1</version>
 <dependencies>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>${mysql.version}</version>
  </dependency>
  <dependency>
   <groupId>p6spy</groupId>
   <artifactId>p6spy</artifactId>
   <version>${p6spy.version}</version>
  </dependency>
 </dependencies>
 <configuration>
  <driver>com.p6spy.engine.spy.P6SpyDriver</driver>
  <url>jdbc:mysql://localhost:3306/TestDB</url>
  <username>root</username>
  <password>password</password>
  <transaction>true</transaction>
 </configuration>
 <executions>
  <execution>
   <phase>test-compile</phase>
   <goals>
    <goal>operation</goal>
   </goals>
   <configuration>
    <format>flat</format>
    <type>CLEAN_INSERT</type>
    <src>src/test/data/insert.xml</src>
   </configuration>
  </execution>
 </executions>
</plugin>

You also need to create the P6Spy configuration (spy.properties) in the project root folder. The example illustrated below shows the minimal configuration required:

module.log=com.p6spy.engine.logging.P6LogFactory
realdriver=com.mysql.jdbc.Driver

Tagged as , + Categorized as Development, Logging

1 Comments

  1. Thank you for this..

    There was an wrong config speficied at http://mojo.codehaus.org/dbunit-maven-plugin/usage.html

    They did not specify the correct config to insert an flat structured xml.. but thanks to ur post now able to clean_Insert

Leave a Reply