上一篇已經體驗過 Maven 強大的程式庫管理,在這一篇會使用到 Maven 另一個功能 - 測試。不可以小看這個測試功能,測試功能能確保你的程式正確,如果你更改了某部份程式碼,可能會影響到整個程式運作,運行一個測試程式可以確保系統其他組件不受影響。Maven 在打包前會自動執行一次測試程式。
使用上一編文章的 Project 再去進行 Maven 練習,打開 pom.xml,然後選擇 Dependenices 標籤,按下 Add 然後輸入 junit 找到一大堆 junit 的程式庫,選擇以下圖片那一個:
按下 OK 後在旁邊的 Dependency Details 的 Scope 選擇 test,因為在主程式內不需要 JUnit,只需在測試程式內使用。
加入一個測試程式去測試 HelloWorldBean 的 getHelloWorld() 是不是返回 “Hello World” 的 String Object:
package com.ctlok.pro.tutorial.maven.javase.test;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ctlok.pro.tutorial.maven.javase.bean.HelloWorldBean;
public class SimpleTest {
private ApplicationContext ctx;
@Before
public void initSpring() {
ctx = new ClassPathXmlApplicationContext(new String[] { "beans.xml" });
}
@Test
public void testHelloWorld() {
HelloWorldBean bean = (HelloWorldBean) ctx
.getBean(HelloWorldBean.class);
Assert.assertEquals("Hello World", bean.getHelloWorld());
}
}
因為我們使用的 JUnit 是 4.8 版本,並使用注解的方式,Java 版本一定要 1.5 或以上才支援,所以加入一個 Plugin 去將程序碼編譯成 Java 1.5 的程式。
現在到 pom.xml 的 Plugins ,加入 maven-compiler-plugin,然後設定以下的 XML:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
完整的 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ctlok.pro.tutorial.maven.javase</groupId>
<artifactId>javase</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Maven Tutorial</name>
<description>Maven Tutorial</description>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<mainClass>com.ctlok.pro.tutorial.maven.javase.Main</mainClass>
<packageName>com.ctlok.pro.tutorial.maven.javase</packageName>
<addClasspath>false</addClasspath>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${pom.url}</url>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>ud</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.4.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.0.4.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.4.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
</dependencies>
</dependencyManagement>
</project>
現在執行 Run as -> Maven test,Console 會打印出以下內容:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Tutorial 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.4.1:resources (default-resources) @ javase ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ javase ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.4.1:testResources (default-testResources) @ javase ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ javase ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.4.3:test (default-test) @ javase ---
[INFO] Surefire report directory: D:\testing\javase\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.ctlok.pro.tutorial.maven.javase.test.SimpleTest
Sep 20, 2010 4:22:06 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@18d107f: startup date [Mon Sep 20 04:22:06 GMT 2010]; root of context hierarchy
Sep 20, 2010 4:22:07 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Sep 20, 2010 4:22:07 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1a33d48: defining beans [helloWorldBean]; root of factory hierarchy
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.354 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.885s
[INFO] Finished at: Mon Sep 20 04:22:07 GMT 2010
[INFO] Final Memory: 2M/4M
[INFO] ------------------------------------------------------------------------
出現這個訊息代表測試成功,你可以試試更改測試程式碼出現不合預期的結果去看看 Maven 會出現什麼訊息。
例如將 SimpleTest.testHelloWorld() 改為以下內容:
HelloWorldBean bean = (HelloWorldBean) ctx
.getBean(HelloWorldBean.class);
Assert.assertEquals("hello world", bean.getHelloWorld());
執行 “Maven clean” 去清理 target 資料夾內所有東西,然後再執行 “Maven test”,Console 會打印出以下內容:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Tutorial 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.4.1:resources (default-resources) @ javase ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ javase ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 2 source files to D:\testing\javase\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.4.1:testResources (default-testResources) @ javase ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ javase ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\testing\javase\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.4.3:test (default-test) @ javase ---
[INFO] Surefire report directory: D:\testing\javase\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.ctlok.pro.tutorial.maven.javase.test.SimpleTest
Sep 20, 2010 4:26:33 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ad3ba4: startup date [Mon Sep 20 04:26:33 GMT 2010]; root of context hierarchy
Sep 20, 2010 4:26:33 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Sep 20, 2010 4:26:33 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b09468: defining beans [helloWorldBean]; root of factory hierarchy
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.323 sec <<< FAILURE!
Results :
Failed tests:
testHelloWorld(com.ctlok.pro.tutorial.maven.javase.test.SimpleTest)
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.812s
[INFO] Finished at: Mon Sep 20 04:26:33 GMT 2010
[INFO] Final Memory: 2M/8M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.4.3:test (default-test) on project javase: There are test failures.
Please refer to D:\testing\javase\target\surefire-reports for the individual test results.
-> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
這就是測試失敗的訊息了,使用 “Maven package” 時,Maven 也會自動去執行一次測試的。現在可以執行 “Maven clean” 去清理 target 資料夾內所有東西,然後再執行 “Maven package” 去打包整個程式。
以上就是簡單的 Maven 應用,你現在應該掌握到 Maven 的基本使用方法,接下來可以試試在沒有 Eclipse 和 m2eclipse 的情況下使用 Maven 去管理 Jave 項目。
Apache Maven 網址: http://maven.apache.org/
相關書籍: