当前位置: 首页 > 知识库问答 >
问题:

在Groovy/Gradle构建中使用JPA复合键时,Spock测试失败

东方嘉木
2023-03-14
@Table(
        name="MyEntity",
        uniqueConstraints=@UniqueConstraint(columnNames=["channel", "service"])
)
public class MyEntity {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    Long id
    String channel
    String service

我开发了一套关于实体的保存、搜索和修改的Spock测试,一切都运行得很顺利。具体地说,我有一个Spring Boot应用程序冒烟测试,以确保配置正确加载

@ContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ApplicationContextSpec extends Specification {

    @Autowired
    WebApplicationContext context

    def "should boot up without errors"() {
        expect: "(web) application context exists"
        context != null
    }
} 

由于自然密钥是channel和service,所以我尝试定义了一个复合密钥

@Table(name="MyEntity")
@IdClass(MyEntityPk.class)
public class MyEntity {

    @Id
    String channel
    @Id
    String service

以及主键类(节选)

public class MyEntityPk implements Serializable {
    String channel
    String service



@ContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ApplicationContextSpec extends Specification {

    @Autowired
    WebApplicationContext context

    def "should boot up without errors"() {
        expect: "(web) application context exists"
        context != null
    }
}
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
...
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
buildscript {
    ext {
        springBootVersion = '1.5.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'

// I am using a mixed environment with some java generated source code
// and mainly groovy classes. This sourceSets definition forces to compile
// everything with groovy compiler so that java classes see groovy classes
sourceSets {
    main {
        groovy {
            srcDirs = ['src/main/groovy', 'src/main/java']
        }
        java {
            srcDirs = []
        }
    }

    test {
        groovy {
            srcDirs = ['src/test/groovy','src/test/java']
        }
        java {
            srcDirs = []
        }
    }
}

repositories {
    mavenCentral()
}

// This task defines a jaxb task that takes xsd schema definition
// and generates java classes used when mapping soap requests
task genJaxb {
    ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
    ext.classesDir = "${buildDir}/classes/jaxb"
    ext.schema = "src/main/resources/myws.xsd"

    outputs.dir classesDir

    doLast() {
        project.ant {
            taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
                    classpath: configurations.jaxb.asPath
            mkdir(dir: sourcesDir)
            mkdir(dir: classesDir)

            xjc(destdir: sourcesDir, schema: schema) {
                arg(value: "-wsdl")
                produces(dir: sourcesDir, includes: "**/*.java")
            }

            javac(destdir: classesDir, source: 1.6, target: 1.6, debug: true,
                    debugLevel: "lines,vars,source",
                    classpath: configurations.jaxb.asPath) {
                src(path: sourcesDir)
                include(name: "**/*.java")
                include(name: "*.java")
            }

            copy(todir: classesDir) {
                fileset(dir: sourcesDir, erroronmissingdir: false) {
                    exclude(name: "**/*.java")
                }
            }
        }
    }
}

configurations {
    jaxb
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-web-services")
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile("org.codehaus.groovy:groovy-all:2.4.7")
    compile("wsdl4j:wsdl4j:1.6.1")
    compile(files(genJaxb.classesDir).builtBy(genJaxb))
    runtime('com.h2database:h2')
    jaxb("org.glassfish.jaxb:jaxb-xjc:2.2.11")
    testCompile 'org.springframework.boot:spring-boot-starter-test'
    testCompile 'org.spockframework:spock-spring:1.0-groovy-2.4'
    testCompile 'cglib:cglib-nodep:3.2.5'
}


jar {
    baseName = 'myws'
    version =  '0.7.1'
    excludes = ['**/application.yml']
    from sourceSets.main.output
    // adding jaxb generated file to be included in jar
    from('build/classes/jaxb') {
        include '**'
    }
}

// this defines a uber-jar so that I may launch spring server 
// from command line using java -jar myws-spring-boot.jar
task('execJar', type:Jar, dependsOn: 'jar') {
    baseName = 'myws'
    version =  '0.7.1'
    classifier = 'spring-boot'
    from sourceSets.main.output
    from('build/classes/jaxb') {
        include '**'
    }

}

bootRepackage  {
    withJarTask = tasks['execJar']
}
gradlew build

构建jar并运行测试,然后

java -jar myws-spring-boot.jar

启动Spring Boot服务器

共有1个答案

易宣
2023-03-14

冒烟测试无法工作,因为当从“长ID”字段作为主键移动时,我没有执行所有所需的更改

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
Long id

@Id
String channel
@Id
String service

我忘记删除repository类中引用“long id”字段的方法。在按照Leonard Brünings的建议转移到Spring-Spock1.1之后,我得到了一个更详细的错误消息,它为我指明了正确的方向。

Error creating bean with name 'modelEntryRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property id found for type ModelEntryEntity!
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
testCompile 'org.spockframework:spock-spring:1.1-groovy-2.4'
 类似资料:
  • 问题内容: 我正在尝试从build.gradle文件中运行Groovy类。我遵循使用指南中的指示,但是出现错误。 构建文件为: 常规类很简单: 但是,当我尝试运行gradlew编译fooTask时,出现以下错误: 无法解析类groovyClass 知道为什么吗? 谢谢 问题答案: 如果要从构建中引用它,则需要添加该类(而不是在简单的Exec任务中)。给定此目录结构: 在哪里: 并且是: 然后,运行

  • 我有一个由Gradle2.4构建的Java库,它将被一些Java6应用程序、一些Java7应用程序、一些Java8应用程序和一些Groovy2.x应用程序使用。因此,为了尽可能地向后兼容,我编写的lib具有和1.6: 但是,我没有理由不能用Groovy/Spock编写单元测试。只要Groovy不是main/compile/runtime类路径的一部分,那么我就可以自由地用任何JVM语言编写测试!我

  • 我如何在我的Grails Spock测试中实现Groovy的新特性?每次我尝试,我都会得到一个看起来像这样的堆栈跟踪。Groovy路径是否有一些我可能不知道的限制? JDK 版本: Groovy Verison: Grails版本: 简化代码: } 堆栈跟踪:

  • 问题内容: 我已经设置了一个Jenkins服务器来运行Selenium测试。生成脚本是用Ant(CentOS 6.3上的v 1.7.1)编写的,并且测试在Sauce Labs上运行。我通过外壳执行构建: 问题是测试失败时,詹金斯(Jenkins)将其标记为成功。我可以通过将设置为来避免这种情况,请参阅: 但是,这并不理想,因为Jenkins会在遇到第一个故障时终止构建。构建完成后是否可以检查失败,

  • 我正在尝试使用Spock运行geb测试。我已经在groovy脚本中编写了所有内容,该脚本位于中,如下所示

  • 我已经设置了一个Jenkins服务器来运行Selenium测试。构建脚本是用Ant(CentOS 6.3上的V1.7.1)编写的,测试在Sauce实验室上运行。我通过shell执行构建: