Maven执行单附加测试

时间:2013-04-06 00:13:24

标签: maven testing

我对maven相对较新,并且一直在努力进行附加测试,我在这里发现了一些文章,这些文章让我在那里得到了部分但却无法完全获得最后一点。这就是我想要做的。我有2个模块,它们定义了一组接口和这些接口的基本测试。第二个模块提供了接口的实现,我想在其上运行预定义的测试。我把它归结为一个简单的例子,这是两个项目:

interface
 + pom.xml
 + src
    + main
       + java
          + demo
             + Messenger.java
    + test
       + java
          + demo
             + MessengerTest.java

impl
 + pom.xml
 + src
    + main
       + java
          + demo
             + impl
                + MessengerImpl.java
    + test
       + resources
          + context.xml

界面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">
  <groupId>demo</groupId>
  <artifactId>interface</artifactId>
  <version>1.0</version>
  <modelVersion>4.0.0</modelVersion>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.2.2.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.14</version>
        <configuration>
          <skipTests>true</skipTests>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.2</version>
        <executions>
          <execution>
            <goals>
              <goal>test-jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Messenger.java来源:

package demo;

public interface Messenger {
    String getMessage();
}

MessengerTest.java来源:

package demo;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import demo.Messenger;

public class MessengerTest {
    private BeanFactory beanfactory;

    @Before
    public void setUp() throws Exception {
        beanfactory = new ClassPathXmlApplicationContext("context.xml");
    }

    @Test
    public final void testGetMessage() throws Exception {
        final Messenger msngr = beanfactory.getBean(Messenger.class);
        String msg = msngr.getMessage();
        assertNotNull(msg);
        assertTrue(msg.length() > 0);
    }
}

执行'mvn install'似乎在我的本地存储库中创建interface-1.0.jar和interface-1.0-tests.jar是正确的。

实现代码很简单,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">
  <groupId>demo</groupId>
  <artifactId>impl</artifactId>
  <version>1.0</version>
  <modelVersion>4.0.0</modelVersion>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>demo</groupId>
      <artifactId>interface</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <groupId>demo</groupId>
      <artifactId>interface</artifactId>
      <version>1.0</version>
      <type>test-jar</type>
      <scope>test</scope>
    </dependency>
  </dependencies>

</project>

MessageImpl.java源:

package demo.impl;

import demo.Messenger;

public class MessengerImpl implements Messenger {

    @Override
    public String getMessage() {
        return "Hello World";
    }
}

最后,弹出上下文文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  <bean id="MyMessenger" class="demo.impl.MessengerImpl"/>
</beans>

完成所有这些后,我执行'mvn -Dtest = demo.MessengerTest test',不幸的是它失败了“......没有执行任何测试!”。所以有问题。

对不起,由于这么长时间的啰嗦,但我想要相当完整,我们将非常感谢任何指导。

1 个答案:

答案 0 :(得分:1)

在确定要运行的类时,Surefire不会扫描类路径,因此通常只考虑在同一模块中定义的测试。它检查target/classes项目中的impl,没有看到任何测试,并忽略MessengerTest。您可以在impl项目中定义一个引用要运行的测试的测试套件,或者将依赖项解压缩到target/classes