Junit4 e TestNG em um projeto com Maven
Pergunta
Para executá-los juntos há poucas opções disponíveis, mas eu escolhi usar perfis diferentes para JUnit e TestNG. Mas agora problema é com excluindo e incluindo casos de teste.
Uma vez que se somarmos testng dependência para projeto principal em maven ele irá pular todos JUnit, eu decidi colocá-lo no perfil separado. Então, eu estou excluindo TestNG testa no padrão (principal) perfil de compilação usando após a entrada em pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<testExcludes>
<testExclude>**/tests/**.*</testExclude>
<testExclude>**/tests/utils/**.*</testExclude>
</testExcludes>
</configuration>
</plugin>
e mesmo para infalível plugin. Então, ele funciona muito bem com o perfil principal e executa somente os testes junit4. Mas quando eu uso perfil testng que costuma executar teste testng mesmo isso não vai compilá-los. Eu estou usando seguinte perfil para executá-los.
<profile>
<id>testNG</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<testIncludes>
<testInclude>**/tests/**.java</testInclude>
<testInclude>**/tests/utils/**.*</testInclude>
</testIncludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<includes>
<include>**/**.class</include>
<include>**/tests/utils/**.class</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.8</version>
<scope>test</scope>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
</profile>
Alguém tem alguma idéia de por que ele não é incluí-los e compilar novamente?
Solução
A configuração para o compilador de encaixe exclui os tipos TestNG. A configuração do perfil é mesclado com a configuração padrão, assim que sua configuração do compilador eficaz é:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<testIncludes>
<testInclude>**/tests/**.java</testInclude>
<testInclude>**/tests/utils/**.*</testInclude>
</testIncludes>
<testExcludes>
<testExclude>**/tests/**.*</testExclude>
<testExclude>**/tests/utils/**.*</testExclude>
</testExcludes>
</configuration>
</plugin>
Isto significa que os seus tipos de TestNG não são sempre compilados e, portanto, não são executados.
Se você especificar o
<testExcludes>
<testExclude>dummy</testExclude>
</testExcludes>
Outras dicas
solução mais simples é como este
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
</plugin>
Mais informações sobre este: mistura testes TestNG e JUnit em um módulo Maven - 2013 edition
Eu não encontrou qualquer solução combinada on-line tanto para Surefire e à prova de falhas. O arquivo POM mudanças abaixo funcionou para mim.
- O truque infalível é feito especificando explicitamente o plugin criança tanto para JUnit e TestNG.
- O truque à prova de falhas é feito definindo duas execuções, cada um com uma configuração vazia, uma para JUnit e o outro para TestNG.
Ambas as soluções são hacks que eu encontrei online. Eu acho que o link para o truque infalível é em outra resposta a esta pergunta.
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe.version}</version>
<executions>
<execution>
<id>test-testng</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<testNGArtifactName>none:none</testNGArtifactName>
</configuration>
</execution>
<execution>
<id>test-junit</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<junitArtifactName>none:none</junitArtifactName>
</configuration>
</execution>
</executions>
</plugin>
<!-- ... -->
</plugins>