darthShana
darthShana

Reputation: 387

groovy spock test unable to find java class in same package

Im pretty new to spock testing and ive come across a hairy problem which i cant figure out.. Im not sure what ive done wrong

I have a simple java class

./src/main/java/com/twg/sample/model/PrimeNumberCalculator.java

package com.twg.sample.model;

import org.springframework.stereotype.Service;
import java.util.stream.IntStream;

@Service
public class PrimeNumberCalculator {

    public int[] getPrimeNumbers(int end) {
        return IntStream.rangeClosed(1, end)
            .filter(number -> !IntStream.rangeClosed(2, number / 2).anyMatch(i -> number % i == 0))
            .toArray();
    }
}

and i have a simple groovy spock test ./src/test/groovy/com/twg/sample/model/PrimeNumberCalculatorSpec.groovy

package com.twg.sample.model

import spock.lang.Specification


class PrimeNumberCalculatorSpec extends Specification{

    def "test prime numbers"(){
        given:
        def primeCal = new PrimeNumberCalculator()

        expect:
        [1, 2, 3, 5, 7] == primeCal.getPrimeNumbers(9)
    }

}

Im using intelliJ and after i mar the src/test/groovy folder as source test root, the test runs fine. However when i do

mvn clean install the test fails

[ERROR] Failed to execute goal org.codehaus.gmavenplus:gmavenplus-plugin:1.5:testCompile (default) on project prime-number-calculator: Error occurred while calling a method on a Groovy class from classpath.: InvocationTargetException: startup failed:
[ERROR] C:\development\prime_number_calculator\src\test\groovy\com\twg\sample\model\PrimeNumberCalculatorSpec.groovy: 10: unable to resolve class PrimeNumberCalculator
[ERROR]  @ line 10, column 24.
[ERROR]            def primeCal = new PrimeNumberCalculator()

why cant the groovy test find the java class which is in the same package? my groovy plugin is

        <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>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <useFile>false</useFile>
                <includes>
                    <include>**/*Spec.groovy</include>
                </includes>
            </configuration>
        </plugin>

Upvotes: 1

Views: 3174

Answers (1)

kriegaex
kriegaex

Reputation: 67297

The problem is a very strange Surefire quirk.

This does not work:

<include>**/*Spec.groovy</include>

Instead any of these does work:

<include>**/*Spec.java</include>
<include>**/*Spec.class</include>
<include>**/*Spec.*</include>

Funny, eh (especially the first variant)? I have not checked if there is an open Surefire ticket for it. You may want to create one and report back here in a comment.

Alternative solution: What I always do is name my Spock tests to end with *Test (Surefire) or *IT (Failsafe). This way I do not need any includes and it will work in projects with mixed Java and Groovy tests.

Upvotes: 1

Related Questions