Testing

Now you’re ready to compile and run the tests. For the first step, you will repeat the previous procedure for the main classes, setting which test resources to use, and setting the JUnit test sources to compile. After compiling the tests, we will cover how to run the tests.

Compiling Tests

Setting the test resources is identical to setting the main resources, with the exception of changing the location from which the element name and directory are pulled. In addition, you will need to add the log4j.properties file required for logging configuration.

 <testResources>
  <testResource>
    <directory>../../test</directory>
    <includes>
      <include>log4j.properties</include>
      <include>org/springframework/core/**</include>
      <include>org/springframework/util/**</include>
    </includes>
    <excludes>
      <exclude>**/*.java</exclude>
    </excludes>
  </testResource>
</testResources>

Setting the test sources for compilation follows the same procedure, as well. Inside the maven-compiler-plugin configuration, you will need to add the testIncludes element.

<testIncludes>
  <include>org/springframework/core/**</include>
  <include>org/springframework/util/**</include>
</testIncludes>

You may also want to check the Log4JConfigurerTests.java class for any hard codes links to properties files and change them accordingly.
Now, if you try to compile the test classes by running mvn test-compile, as before, you will get compilation errors, but this time there is a special case where the compiler complains because some of the classes from the org.springframework.mock, org.springframework.web and org.springframework.beans packages are missing. It may appear initially that spring-core depends on spring-mock, spring-web and spring-beans modules, but if you try to compile those other modules, you will see that their main classes, not tests, depend on classes from spring-core. As a result, we cannot add a dependency from spring-core without creating a circular dependency. In other words, if spring-core depends on spring-beans and spring-beans depends on spring-core, which one do we build first? Impossible to know.
So, the key here is to understand that some of the test classes are not actually unit tests for spring-core, but rather require other modules to be present. Therefore, it makes sense to exclude all the test classes that reference other modules from this one and include them elsewhere.
To exclude test classes in Maven, add the testExcludes element to the compiler configuration as follows.

 <testExcludes>
  <exclude>org/springframework/util/comparator/ComparatorTests.java</exclude>
  <exclude>org/springframework/util/ClassUtilsTests.java</exclude>
  <exclude>org/springframework/util/ObjectUtilsTests.java</exclude>
  <exclude>org/springframework/util/ReflectionUtilsTests.java</exclude>
  <exclude>org/springframework/util/SerializationTestUtils.java</exclude>
  <exclude>org/springframework/core/io/ResourceTests.java</exclude>
</testExcludes>

Now, when you run mvn test-compile, you will see the following error:

package javax.servlet does not exist

This means that the following dependency must be added to the POM, in order to compile the tests:

 <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.4</version>
  <scope>test</scope>
</dependency>

The scope is set to test, as this is not needed for the main sources.
If you run mvn test-compile again you will have a successful build, as all the test classes compile correctly now.

Running Tests

Running the tests in Maven, simply requires running mvn test. However, when you run this command, you will get the following error report:

Results :
[surefire] Tests run: 113, Failures: 1, Errors: 1

[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] There are test failures.
[INFO] ------------------------------------------------------------------------

Upon closer examination of the report output, you will find the following:

[surefire] Running org.springframework.core.io.support.PathMatchingResourcePatternResolverTests
[surefire] Tests run: 5, Failures: 1, Errors: 1, Time elapsed: 0.015 sec <<<<<<<< FAILURE !!

This output means that this test has logged a JUnit failure and error. To debug the problem, you will need to check the test logs under target/surefire-reports, for the test class that is failing org.springframework.core.io.support.PathMatchingResourcePatternResolverTests.txt. Within this file, there is a section for each failed test called stacktrace.
The first section starts with java.io.FileNotFoundException: class path resource [org/aopalliance/] cannot be resolved to URL because it does not exist.
This indicates that there is something missing in the classpath that is required to run the tests. The org.aopalliance package is inside the aopalliance JAR, so to resolve the problem add the following to your POM

<dependency>
  <groupId>aopalliance</groupId>
  <artifactId>aopalliance</artifactId>
  <version>1.0</version>
  <scope>test</scope>
</dependency>

Now run mvn test again. You will get the following wonderful report:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------

The last step in migrating this module (spring-core) from Ant to Maven, is to run mvn install to make the resulting JAR available to other projects in your local Maven repository. This command can be used instead most of the time, as it will process all of the previous phases of the build life cycle (generate sources, compile, compile tests, run tests, etc.)

Thank you for requesting a Maestro evaluation! This is our passion, and we want you to be successful. Please let us know how we may help!

Please enter your name, company email address and phone, and we will send you a link to your pre-built hosted evaluation within minutes.






I have read and agree to the Terms and Conditions.