通过Maven去运行单元测试

通过Maven去运行单元测试,运行下边的命令

mvn test                 

他会运行你项目中的所有单元测试类

示范例子
创建两个单元测试类并且通过macen去运行他们,下面是一个简单的待测试类

package com.sniper.core;
public class App { public static void main(String[] args) { System.out.println(getHelloWorld()); } public static String getHelloWorld() { return "Hello World"; } public static String getHelloWorld2() { return "Hello World 2"; } }

单元测试1
对getHelloWorld()的方法的单元测试

package com.sniper.core;

import junit.framework.Assert;
import org.junit.Test;
 
public class TestApp1 {
    @Test
    public void testPrintHelloWorld() {
        Assert.assertEquals(App.getHelloWorld(), "Hello World");
    }
}

单元测试2
对getHelloWorld2()方法的单元测试

package com.sniper.core;

import junit.framework.Assert;
import org.junit.Test;
 
public class TestApp2 {
    @Test
    public void testPrintHelloWorld2() {
        Assert.assertEquals(App.getHelloWorld2(), "Hello World 2");
    }
}

运行单元测试
看下面基于Maven的单元测试运行

例子1
要运行整个单元测试(TestApp1和TestApp2),发出以下命令

mvn test                  

例子2
要运行单个测试(TestApp1),发出下面的命令

mvn -Dtest=TestApp1 test

例子3
要运行单个测试(TestApp1),发出下面的命令

mvn -Dtest=TestApp2 test

注意
更多"mvn test"的例子, 请参考文档maven-test plugin 

posted @ 2012-10-31 21:27  Mr-sniper  阅读(482)  评论(0编辑  收藏  举报