Search code examples
javaspringmavenjarbuildpath

unit testing Spring Web app with JConnect


I am having an issue with my attempt to unit test my DAO in a webapp. I have my spring configuration all set up to create a dataSource bean using the Sybase JConnect JDBC driver. My problem is that I can only get my bean created when I run the app as a webapp.

In trying to run unit tests, I receive:

java.lang.ClassNotFoundException: com.sybase.jdbc3.jdbc.SybDriver

To further explain, here is my directory structure:

src
   main
     -java
       - ...java files etc
    - resources
        -applicationContext.xml
        -other config files
    - webapp
        -WEB-INF
           -jconn3.jar   <---- putting it here works, but test doesn't have a WEB-INF folder!
   -test
     -java
     -resources

So how do I allow jconn3.jar to be recognized at runtime for my unit tests? I have tried putting it in the main/resources directoru, but this causes the failure regardless of whether I'm running it as webapp or not. I can see that jconn3.jar gets copied to the target/classes directory on build, so why is the jar not found at runtime? It seems the only way to get this to work is to keep it in the WEB-INF directory, but then how do I unit test my DAO that depends on it.

I am using Spring MVC and maven. Note I cannot add a maven dependency since the jar is not in a repository, nor do I have the option to add it to a remote repo.

So to sum up, I need to have the jar on my build path for both running the webapp AND for unit testing since both need the jar for the DAO. How do I achieve this when placing it in the resources folder doesn't seem to help.


Solution

  • You have to put jconn3.jar in the execution classpath of your tests. If you're using maven as it seems you're doing you have to add the dependency corresponding to your driver in your pom. Since the Sybase JDBC driver is not available in the central Maven you will have to upload it to your local repository using a command like this:

    Guide to installing 3rd party JARs

    mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
        -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
    

    Once you have done this you can add a dependency to this jar the same way you would do for any other jar in maven central

    <dependency>
        <groupId>group-id></groupId>
        <artifactId>artifact-id</artifactId>
        <version>version</version>
    </dependency>
    

    From that point on your Sybase driver jar will be included in the tests execution classpath...

    You are also adding the jar to the WEB-INF directory and you don't need to. Maven is smart enough to deduce from your dependencies the jar files that need to go inside your war so you can remove it from that WEB-INF location. Don't worry, when you execute mvn package the jar with the driver will wind up in your war file