Compiling Test Sources and Running Unit Tests
Now that you’re successfully compiling your application’s sources, you probably have unit tests that you want to compile and execute as well (after all, programmers always write and execute their own unit tests nudge nudge, wink wink).
Again, simply tell Maven you want to test your sources. This implies that all prerequisite phases in the life cycle will be performed to ensure that testing will be successful. Use the following simple command to test:
C:mvnbookmy-app> mvn test
After executing this command you should see output similar to the following:
[INFO]------------------------------------------------------ ------------- [INFO] Building Maven Quick Start Archetype [INFO] task-segment: [test] [INFO]------------------------------------------------------ ------------- [INFO] artifact org.apache.maven.plugins:maven-surefire-plugin: checking for updates from central ... [INFO] [resources:resources] [INFO] [compiler:compile] [INFO] Nothing to compile - all classes are up to date [INFO] [resources:testResources] [INFO] [compiler:testCompile] Compiling 1 source file to C:TestMaven2testmy-apptargettest-classes ... [INFO] [surefire:test] [INFO] Setting reports dir: C:TestMaven2testmy-apptarget/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- [surefire] Running com.mycompany.app.AppTest [surefire] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0 sec Results : [surefire] Tests run: 1, Failures: 0, Errors: 0 [INFO]------------------------------------------------------ ------------- [INFO] BUILD SUCCESSFUL [INFO]------------------------------------------------------ ------------- [INFO] Total time: 15 seconds [INFO] Finished at: Thu Oct 06 08:12:17 MDT 2005 [INFO] Final Memory: 2M/8M [INFO]------------------------------------------------------ -------------
Some things to notice about the output:
- Maven downloads more dependencies this time. These are the dependencies and plugins necessary for executing the tests (recall that it already has the dependencies it needs for compiling and won’t download them again).
- Before compiling and executing the tests, Maven compiles the main code (all these classes are up-to-date, since we haven’t changed anything since we compiled last).
If you simply want to compile your test sources (but not execute the tests), you can execute the following command:
C:mvnbookmy-app> mvn test-compile
However, remember that it isn’t necessary to run this every time; mvn test will always run the compile and test-compile phases first, as well as all the others defined before it.
Now that you can compile the application sources, compile the tests, and execute the tests, you’ll want to move on to the next logical step, how to package your application.