簡體   English   中英

groovy spock測試無法在同一包中找到java類

[英]groovy spock test unable to find java class in same package

我對Spock測試和ive相當陌生,但遇到一個毛發問題,我無法弄清楚。.我不確定ive的錯是什么

我有一個簡單的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();
    }
}

我有一個簡單的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)
    }

}

我使用intelliJ,然后將src / test / groovy文件夾作為源測試根目錄進行了測試,測試運行正常。 但是當我這樣做

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

為什么groovy測試找不到同一包中的java類? 我的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>

問題是一個非常奇怪的Surefire怪癖。

不起作用

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

相反,這些方法中的任何一種都起作用:

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

有趣,是嗎(尤其是第一個變體)? 我沒有檢查是否有開放的Surefire票。 您可能要創建一個,然后在評論中返回此處。

替代解決方案:我經常做的就是將我的Spock測試命名為*Test (Surefire)或*IT (Failsafe)。 這樣,我不需要任何包含,它將在具有Java和Groovy混合測試的項目中工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM