简体   繁体   English

groovy spock测试无法在同一包中找到java类

[英]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 我对Spock测试和ive相当陌生,但遇到一个毛发问题,我无法弄清楚。.我不确定ive的错是什么

I have a simple java class 我有一个简单的java类

./src/main/java/com/twg/sample/model/PrimeNumberCalculator.java ./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 我有一个简单的Groovy Spock测试./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. 我使用intelliJ,然后将src / test / groovy文件夹作为源测试根目录进行了测试,测试运行正常。 However when i do 但是当我这样做

mvn clean install the test fails mvn clean install测试失败

[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? 为什么groovy测试找不到同一包中的java类? my groovy plugin is 我的groovy插件是

        <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>

The problem is a very strange Surefire quirk. 问题是一个非常奇怪的Surefire怪癖。

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. 我没有检查是否有开放的Surefire票。 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). 替代解决方案:我经常做的就是将我的Spock测试命名为*Test (Surefire)或*IT (Failsafe)。 This way I do not need any includes and it will work in projects with mixed Java and Groovy tests. 这样,我不需要任何包含,它将在具有Java和Groovy混合测试的项目中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM