User5817351
User5817351

Reputation: 1007

Running Spock unit test along with TestNG in Spring boot maven based project

I'm working with IntelliJ, Spring Boot 1.3, java8 and maven.

We were using TestNG for our unit but when I started using Spock for unit test, I really liked writing test using Spock in Groovy. Here is the folder structure:

src->test->java -> All java based TestNG test lives here
src->test->groovy -> All groovy based Spock test lives here. All test have a suffix of Spec and that's how I've configured surefire plugin to look at those test.
Now when I run these individual test they work just fine. But when I run maven lifecycle command like mvn test or mvn clean install, Spock test does not run even though I have enabled maven compiler and appropriate library.

This is how my pom looks like:

 <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${version.surefire}</version>
                <configuration>
                    <skipTests>false</skipTests>
                    <useFile>false</useFile>
                    <includes>
                        <include>**/*Spec.*</include>
                        <include>**/*Test.java</include>
                    </includes>
                </configuration>

            </plugin>

            <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-core</artifactId>
        <version>1.0-groovy-2.4</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib-nodep</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency> <!-- enables mocking of classes without default
        constructor (together with CGLIB) -->
        <groupId>org.objenesis</groupId>
        <artifactId>objenesis</artifactId>
        <version>2.1</version>
        <scope>test</scope>
    </dependency>    

I think I'm missing something in plugin which would let maven run Spock test along with TestNG during maven lifecycle command. I think I'm missing trivial thing here. Can can someone give me some pointers here on what should I add in pom or if someone has a sample skeleton project in github which I can look at. Thanks.

Upvotes: 2

Views: 929

Answers (1)

User5817351
User5817351

Reputation: 1007

I was able to get this working. Here is my updated pom.

I had two problems:

-One to run TestNG and Groovy test as a part of maven test lifecycle. This was made possible by adding dependency in surefire plugin. See below pom.

-After fixing that I had a problem another problem with Intellij where it was complaining about class already exists for my groovy based test. This was due to generated stubs in target folder. For this I had to provide configuration to override those directories as suggested by below link.

<plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <goals>
                        <goal>addTestSources</goal>
                        <goal>testGenerateStubs</goal>
                        <goal>testCompile</goal>
                        <goal>removeTestStubs</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!--<This setting is for intellij bug. This override default location of groovy stubs. for more info
                check this : https://youtrack.jetbrains.com/issue/IDEA-153779>-->
                <stubsOutputDirectory>${project.build.directory}/generated-groovy-stubs</stubsOutputDirectory>
                <testStubsOutputDirectory>${project.build.directory}/generated-groovy-test-stubs</testStubsOutputDirectory>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${version.surefire}</version>
            <configuration>
                <includes>
                    <include>**/*Spec*.groovy</include>
                    <include>**/*Test*.java</include>
                </includes>
                <properties>
                    <property>
                        <name>junit</name>
                        <value>false</value>
                    </property>
                </properties>
                <threadCount>1</threadCount>
            </configuration>
            <!--Below dependency let's surefire play nice with groovy test and testng tests-->
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>${version.surefire}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-testng</artifactId>
                    <version>${version.surefire}</version>
                </dependency>
            </dependencies>
        </plugin>

I hope this helps other folks who are trying to run TestNG and Spock test together.

Upvotes: 1

Related Questions