Maven сообщает, что запускал набор тестов, но не запускал ни одного из моих тестов JUnit. - PullRequest
4 голосов
/ 19 июля 2011

Я пытаюсь протестировать Java-приложение от Maven с помощью JUnit.

У меня есть следующий POM (только соответствующий раздел).

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit4</artifactId>
                        <version>2.7.2</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include>
                    </includes>
                </configuration>
  </plugin>

Кажется, "что-то" запускается. Вот вывод из "mvn -X test":

Running TestSuite
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.769 sec

Results :


Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

Обратите внимание на «Running TestSuite». Однако он все еще не может найти мои методы испытаний. Я подозреваю, что это может быть связано с этим:

[DEBUG] Adding to surefire test classpath: /home/julien/.m2/repository/org/apache/maven/surefire/surefire-testng/2.4.3/surefire-testng-2.4.3.jar
[DEBUG] Adding to surefire test classpath: /home/julien/.m2/repository/org/apache/maven/maven-artifact/2.0/maven-artifact-2.0.jar
[DEBUG] Adding to surefire test classpath: /home/julien/.m2/repository/org/codehaus/plexus/plexus-utils/1.0.4/plexus-utils-1.0.4.jar
[DEBUG] Adding to surefire test classpath: /home/julien/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar
[DEBUG] Adding to surefire test classpath: /home/julien/.m2/repository/org/apache/maven/surefire/surefire-api/2.4.3/surefire-api-2.4.3.jar

Он говорит, что использует версию 3.8.1 Junit ...

Однако он продолжает:

Test Classpath:

 [DEBUG]   /home/julien/.m2/repository/junit/junit/4.8.2/junit-4.8.2.jar

Интересно, в чем разница между "верным испытательным путем к классам" и "испытательным путем к классам"?

Вот как мой метод тестирования назван и аннотирован:

 @Test
    public void test() {

...

Находится здесь:

src/test/java/com/cheetah/web/pages

Есть идеи, что я ошибаюсь?

Вот эффективный POM:

<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>
  <parent>
    <artifactId>cheetah</artifactId>
    <groupId>com.cheetah</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.cheetah</groupId>
  <artifactId>cheetah-web</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>cheetah-web</name>
  <build>
    <sourceDirectory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/src/main/java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/src/test/java</testSourceDirectory>
    <outputDirectory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/target/classes</outputDirectory>
    <testOutputDirectory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/target/test-classes</testOutputDirectory>
    <resources>
      <resource>
        <mergeId>resource-2</mergeId>
        <directory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/src/main/java</directory>
        <includes>
          <include>**/*.tml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
      <resource>
        <mergeId>resource-3</mergeId>
        <directory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/src/main/resources</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <mergeId>resource-1</mergeId>
        <directory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/src/test/resources</directory>
      </testResource>
    </testResources>
    <directory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/target</directory>
    <finalName>cheetah-web-1.0-SNAPSHOT</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-4</version>
        </plugin>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>2.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.4</version>
        </plugin>
        <plugin>
          <artifactId>maven-ear-plugin</artifactId>
          <version>2.3.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-ejb-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-javadoc-plugin</artifactId>
          <version>2.5</version>
        </plugin>
        <plugin>
          <artifactId>maven-plugin-plugin</artifactId>
          <version>2.5</version>
        </plugin>
        <plugin>
          <artifactId>maven-rar-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.0-beta-8</version>
        </plugin>
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>2.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-source-plugin</artifactId>
          <version>2.0.4</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.4.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>2.1-beta-1</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1-beta-1</version>
      </plugin>
      <plugin>
        <artifactId>maven-help-plugin</artifactId>
        <version>2.1.1</version>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <id>java.net</id>
      <url>http://download.java.net/maven/1</url>
      <layout>legacy</layout>
    </repository>
    <repository>
      <id>maven2-repository.dev.java.net</id>
      <name>Java.net Repository for Maven</name>
      <url>http://download.java.net/maven/2</url>
    </repository>
    <repository>
      <id>jboss</id>
      <name>jboss</name>
      <url>http://repository.jboss.org/maven2/</url>
    </repository>
    <repository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <updatePolicy>never</updatePolicy>
      </snapshots>
      <id>jboss-public-repository-group</id>
      <name>JBoss Public Maven Repository Group</name>
      <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
    </repository>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <url>http://repo1.maven.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <updatePolicy>never</updatePolicy>
      </snapshots>
      <id>jboss-public-repository-group</id>
      <name>JBoss Public Maven Repository Group</name>
      <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
    </pluginRepository>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Plugin Repository</name>
      <url>http://repo1.maven.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <dependencies>
    <dependency>
      <groupId>com.cheetah</groupId>
      <artifactId>cheetah-domain</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>com.cheetah</groupId>
      <artifactId>cheetah-service</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tapestry</groupId>
      <artifactId>tapestry-core</artifactId>
      <version>5.2.6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tapestry</groupId>
      <artifactId>tapestry-beanvalidator</artifactId>
      <version>5.2.6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tapestry</groupId>
      <artifactId>tapestry-spring</artifactId>
      <version>5.2.6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.tapestry</groupId>
      <artifactId>tapestry-test</artifactId>
      <version>5.2.6</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>3.0.5.RELEASE</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.easymock</groupId>
      <artifactId>easymock</artifactId>
      <version>2.5.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-web-api</artifactId>
      <version>6.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <reporting>
    <outputDirectory>/home/julien/Documents/donnees/projets/site-emploi-finance/java/cheetah/web/target/site</outputDirectory>
  </reporting>
</project>

Ответы [ 3 ]

2 голосов
/ 19 июля 2011

Выполнение тестов Junit в Maven требует минимальной настройки. Добавьте junit в ваши зависимости:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.7</version>
    <scope>test</scope>
</dependency>

Создание класса в src / test / java / com / cheetah / web / pages:

public class FooTest {
    @Test
    public void writingToStdOut_succeeds() throws Exception {
        System.out.println("I work!");
    }
}

Выполнить mvn test. Это все. Нет необходимости настраивать что-либо в плагине surefire. Это ничем не отличается от проекта войны против проекта фляги. Если вы делаете эти две вещи, и это не работает, то что-то неправильно настроено где-то еще, и нам, вероятно, нужно будет увидеть весь пом или, еще лучше, эффективный пом , чтобы сказать, что происходит .

Обратите внимание, что Surefire будет запускать только тестовые тесты или тесты junit, но не оба в одной сборке.

1 голос
/ 25 августа 2016

Просто чтобы упомянуть, что все уже знают, но то, что все еще ловит меня время от времени.Помните, что существует соглашение о присвоении имен тем, какие тесты будут «видны», и если вы неправильно называете тестовый класс, он игнорируется.

running

mvn -X test

показывает нам (обрезается):

[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test' with basic configurator -->
[DEBUG]   (s) additionalClasspathElements = []
...
[DEBUG]   (s) excludes = [**/Abstract*.java]
[DEBUG]   (s) includes = [**/*Tests.java, **/*Test.java]

Таким образом, по умолчанию будут выбраны только классы, заканчивающиеся на «Test.java» или «Tests.java».

0 голосов
/ 14 октября 2016

В моем случае проблема была в зависимости от версии maven. Я добавил его вручную и допустил ошибку.

Совет:

Вы всегда должны добавлять зависимости с помощью меню «Вставить зависимость». Это можно отобразить с помощью сочетания клавиш «crtl» + «пробел», когда курсор находится между <dependency> и </dependency> в вашем XML-файле. Когда вы в меню «Выберите зависимость», введите «junit» в текстовом поле «Enter ...» и выберите свою версию junit. После нажатия «ОК» ваша зависимость будет автоматически добавлена. Это помогает избежать проблем с неверной версией, набранной вручную.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...