Running test with PaxExam 3.x - TestNg

811 views Asked by At

Edited to simplify the example...

I'm migrating the system to Maven. I want to use PaxExam to run the test with TestNg.

I'm trying to run a simple test using PaxExam:

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>MyTest</groupId>
    <artifactId>PaxExam</artifactId>
    <version>0.0.2</version>
    <packaging>jar</packaging>
    <name>Prove PaxExam</name>

    <properties>
        <exam.version>3.3.0</exam.version>
        <url.version>1.6.0</url.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.ops4j.pax.exam</groupId>
            <artifactId>pax-exam-container-native</artifactId>
            <version>${exam.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.ops4j.pax.exam</groupId>
            <artifactId>pax-exam-testng</artifactId>
            <version>${exam.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.ops4j.pax.exam</groupId>
            <artifactId>pax-exam-link-mvn</artifactId>
            <version>${exam.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.ops4j.pax.url</groupId>
            <artifactId>pax-url-aether</artifactId>
            <version>${url.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.framework</artifactId>
            <version>4.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>0.9.29</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>0.9.29</version>
            <scope>test</scope>
        </dependency>
            <dependency>
        <groupId>org.ow2.spec.ee</groupId>
        <artifactId>ow2-jta-1.1-spec</artifactId>
        <version>1.0.12</version>
        <scope>test</scope>
    </dependency>   
    </dependencies>
</project>

Java program:

   package MyTest.PaxExam;
    import static org.ops4j.pax.exam.CoreOptions.*;
    import static org.testng.Assert.*;
    import javax.inject.Inject;
    import org.ops4j.pax.exam.Option;
    import org.ops4j.pax.exam.Configuration;
    import org.ops4j.pax.exam.ConfigurationFactory;
    import org.ops4j.pax.exam.spi.reactors.PerClass;
    import org.ops4j.pax.exam.testng.listener.PaxExam;
    import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
    import org.osgi.framework.Bundle;
    import org.osgi.framework.BundleContext;
    import org.testng.annotations.Listeners;
    import org.testng.annotations.Test;
    import ch.qos.logback.classic.*;
    import org.slf4j.LoggerFactory;

    @Listeners(PaxExam.class)
    @ExamReactorStrategy(PerClass.class)
    public class Prove {

        @Inject
        BundleContext bc;

        @Configuration
        public Option[] config() {
            System.out.println("config() called");
            return options(mavenBundle("org.testng","testng","6.3.1"));         
        }


        @org.testng.annotations.Test
        public void checkInject() {
            System.out.println("checkInject() called");
            assertNotNull(bc);
        }   
    }

The results that I have, when I verify the test, are:

    [INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Prove PaxExam 0.0.2
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ PaxExam ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\users\dan\desktop\acf\MyTest2\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ PaxExam ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ PaxExam ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ PaxExam ---
[INFO] Compiling 1 source file to C:\users\dan\desktop\acf\MyTest2\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ PaxExam ---
[INFO] Surefire report directory: C:\users\dan\desktop\acf\MyTest2\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

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

[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ PaxExam ---
[INFO] Building jar: C:\users\dan\desktop\acf\MyTest2\target\PaxExam-0.0.2.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.246s
[INFO] Finished at: Fri Dec 06 16:33:14 CET 2013
[INFO] Final Memory: 15M/226M
[INFO] ------------------------------------------------------------------------

The container is not started, no test is run.

2

There are 2 answers

0
Cristina Galán On BEST ANSWER

Finally!

According to the surefire plugin documention : http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html

As long as they are named using the defaults such as *Test.java they will be run by Surefire as TestNG tests.

If you'd like to use a different naming scheme, you can change the includes parameter, as discussed in the Inclusions and Exclusions of Tests example.

My test class name did not start with Test nor end with Test. Changing the class name to TestProve.java solves the problem.

Thanks to hwellmann for his time.

5
Harald Wellmann On

Not sure what you're trying to do. If you're trying to run OSGi tests with an embedded OSGi framework, don't use the exam-maven-plugin.

If you're trying to run plain old unit/integration tests communicating with an external OSGi application, you can use the exam-maven-plugin to launch that application in Pax Exam server mode, but then there's no need to use Pax Exam in your tests.