【问题标题】:Groovy tests on Java classJava 类的 Groovy 测试
【发布时间】:2014-02-21 19:54:53
【问题描述】:

我正在使用 Spock 测试一个 Java 类。

当两者都在“默认”包中时,我在 IntelliJ IDEA 中收到“没有此类属性”错误,如果我没记错的话,它在 Eclipse 中运行良好。 导入Java类解决了这个问题。

这行得通:

def var = new MyClass()
def result = var.doStuff()

虽然没有,但没有静态导入:

def result = MyClass.doStuff()

有没有办法在没有隐式导入的情况下解决这个问题?

【问题讨论】:

    标签: groovy spock


    【解决方案1】:

    不确定您的意思-也许您可以提供更多详细信息。我创建了以下示例 java 类和一个非常短的 spock 测试,没有任何问题。

    /**
     * Created by mike on 1/29/14.
     */
    public class SampleJava {
    
    private int count;
    private String text;
    
    public SampleJava(int cnt, String input) {
        count = cnt;
        text = input;
    }
    
    @Override
    public String toString() {
        return "SampleJava{" +
                "count=" + count +
                ", text='" + text + '\'' +
                '}';
    }
    
    public int getCount() {
        return count;
    }
    
    public void setCount(int count) {
        this.count = count;
    }
    
    public String getText() {
        return text;
    }
    
    public void setText(String text) {
        this.text = text;
    }
    
        public static void doStuff() {
            System.out.println("doStuff...");
        }
    
    public static void main(String[] args) {
        SampleJava sj = new SampleJava(5, "Hello");
        System.out.println(sj);
    }
    }
    

    这是斯波克测试

    /**
     * Created by mike on 1/29/14.
     */
    import  spock.lang.Specification
    
    class TestSampleJava extends Specification {
    
    SampleJava sampleJava
    
    def "test constructor"() {
        sampleJava = new SampleJava(5, "Hello")
    
        expect:
        sampleJava.count == 5
        sampleJava.text == 'Hello'
        SampleJava.doStuff()
       }
    
    }
    

    【讨论】:

    • 查看我更新的问题。问题在于对静态方法的调用。另外,我认为它在 Eclipse 中运行良好,但现在我在 IntelliJ 中,它给了我“没有这样的属性”错误。
    猜你喜欢
    • 2016-07-13
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    相关资源
    最近更新 更多