简体   繁体   English

Eclipse找不到适用于Spock / Junit测试的常规类

[英]Eclipse cannot find groovy class for Spock/Junit test

I am working on using spock in eclipse, and when trying to run tests I had successfully run in intelliJ I came across an issue. 我正在使用eclipse中的spock进行操作,当尝试运行已在intelliJ中成功运行的测试时,遇到了一个问题。 I had named my class different then my file name and so eclipse is unable to run a junit test and instead gives this error: 我用与文件名不同的类来命名我的类,因此eclipse无法运行junit测试,而是出现此错误: 在此处输入图片说明 "The Input type of the launch configuration does not exist." “启动配置的输入类型不存在。”

After some googling I got no where and decided to play around and found that if I executed all the test it would run fine, and even when I changed the class name to match the filename it would work. 经过一番谷歌搜索后,我无处可去,决定玩转,发现如果我执行所有测试,它将运行正常,即使我更改类名以匹配文件名也可以。 Is there a way to configure eclipse/junit to be able to run a test on a groovy class that does not match the file name? 有没有一种方法可以将eclipse / junit配置为能够在与文件名不匹配的groovy类上运行测试?

HelloWorld.groovy: HelloWorld.groovy:

import spock.lang.*

class MyFirstSpockTest extends spock.lang.Specification {

def "length of Spock's and his friends' names"() {
    expect:
    name.size() == length

    where:
    name     | length
    "Spock"  | 5
    "Kirk"   | 4
    "Scotty" | 6
}

def "basic test"() {
    expect:
    Math.min(a, b) == c

    where:
    a | b || c
    3 | 7 || 7   // Will fail here
    5 | 4 || 4   // Will pass
    9 | 9 || 9   // Will pass
}

def "persons name tells you gender"() {
    expect:
    person.getGender() == gender

    where:
    person                    || gender
    new Person(name: "Fred")  || "Male"
    new Person(name: "Wilma") || "Female"
}

static class Person {
    String name

    String getGender() {
        name.equals("Fred") ? "Male" : "Female"
    }
}
}

The root of both your main code tree and test code tree need to be in the classpath settings. 主代码树和测试代码树的根都需要在类路径设置中。 To do this, in the Package Explorer window, right click on the Project name, select "Build Path" from the menu, then select "Configure Build Path", select the Source tab and make sure both your main code and test code directories are listed. 为此,在Package Explorer窗口中,右键单击Project名称,从菜单中选择“ Build Path”,然后选择“ Configure Build Path”,选择Source选项卡,并确保主代码和测试代码目录都位于列出。 If the test code directory is not listed, add it. 如果未列出测试代码目录,请添加它。

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

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