maciejka
maciejka

Reputation: 948

Use jenkins platform to run spock tests in maven project

I have created maven project which consists of junit and spock tests. Code of both tests.

public class AppTest 
{
    /**
     * Rigorous Test :-)
     */
    @Test
    public void shouldAnswerWithTrue()
    {
        assertTrue( true );
    }
}

class SpockTest extends Specification  {

    def "one plus one should equal two"() {
        expect:
        1 + 1 == 2
    }

}

In my local machine when I run mvn test , it decects both test classes. I deployed project on git repository and configured jenkins for maven project. I push on repository and execute job for this project, however jenkins detect only AppTest class for JUnit test. I have changed pom.xml and added file regadring to https://github.com/menonvarun/testInProgress-spock-client. My project structure.

enter image description here

content of file org.spockframework.runtime.extension.IGlobalExtension

org.imaginea.jenkins.testinprogress.spock.SpockTestInProgressExtension

pom.xml

<repositories>
    <repository>
      <id>repo.jenkins-ci.org</id>
      <url>http://repo.jenkins-ci.org/public/</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.spockframework</groupId>
      <artifactId>spock-core</artifactId>
      <version>1.0-groovy-2.4</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.4.7</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.imaginea.jenkins.plugins</groupId>
      <artifactId>testInProgress-spock-client</artifactId>
      <version>0.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

I installed on jenkins platform testInProgress plugin. After that I push changed maven project onto repository and execute again job for maven project. Another time jenkins does not detect SpockTest class. What could be the problem?

Upvotes: 0

Views: 470

Answers (1)

Dmytro Maslenko
Dmytro Maslenko

Reputation: 2297

Try to put spock tests under groovy folder:

  • src
    • test
      • groovy
        • com.jenk...
          • SpockTest.groovy

Then add gmavenplus-plugin (groovy compiler) and maven-surefire-plugin (test runner) into pom.xml:

    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.6.2</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
            </plugin>
        </plugins>
    </pluginManagement>

    ...

    <plugins>
        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <configuration>
                <targetBytecode>1.8</targetBytecode>
                <warningLevel>2</warningLevel>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compileTests</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
        </plugin>
    </plugins>

For debugging on Jenkins to make sure the expected test is triggered better to have failed condition:

def "one plus one should equal two"() {
    expect:
    1 + 1 == 3
}

Upvotes: 2

Related Questions