为什么包含Kafka会破坏我的Spring控制器的单元测试?

时间:2017-02-16 13:23:07

标签: spring spring-mvc spring-boot apache-kafka

我正在创建一个将使用Kafka的Spring Boot Web应用程序。我创建了一个简单的控制器,它将返回一个字符串。

// src/main/groovy/RootController.groovy

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ResponseBody


/**
 * Controller to serve the index page
 */
@Controller
class RootController {

    /**
     * Serves the index page
     *
     * @return A string to appear on the index page
     */

    @GetMapping("/")
    @ResponseBody
    String loadPage() {
        return "root/index"
    }

}

我有一个使用Spock的单元测试。

// src/test/groovy/RootControllerTest.groovy

import org.springframework.test.context.web.WebAppConfiguration
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import spock.lang.Specification

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebAppConfiguration
class RootControllerTest extends Specification {

    MockMvc mockMvc

    // Setup for tests happens here˚
    void setup() {
        mockMvc = MockMvcBuilders.standaloneSetup(new RootController()).build()
    }

    // Cleanup for tests happens here
    void cleanup() {}

    def "LoadPage should return the string root/index"() {
        when: 'the loadPage endpoint is hit'
        def response = mockMvc.perform(get("/"))

        then: 'the method loadPage returns the string root/index'
        response.andExpect(status().isOk())
                .andExpect(content().string("root/index"))

    }

}

运行时,此测试通过。我有以下build.gradle文件,它将构建并运行测试。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE")
    }
}

plugins {
    id 'org.springframework.boot' version '1.5.1.RELEASE'   // Spring Boot
}

group 'dummy'
version '1.0-SNAPSHOT'

task wrapper(type: Wrapper) {
  gradleVersion = '3.1'
  distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
}

apply plugin: 'groovy'

sourceCompatibility = 1.8
def kafkaVersion = '0.10.0.0'

repositories {
    maven {
        name "confluent"
        url "http://packages.confluent.io/maven/"
    }
    mavenCentral()
}

dependencies {
    compile "org.codehaus.groovy:groovy-all:2.4.4"                      // Groovy version
    compile "org.springframework.boot:spring-boot-starter-thymeleaf"    // For thymeleaf


    //compile "org.apache.kafka:kafka_2.11:${kafkaVersion}-cp1"         // Kafka, as produced by confluent, hence -cp1 suffix


    // Testing dependencies go here
    testCompile "org.springframework:spring-test:4.3.6.RELEASE"
    testCompile "org.spockframework:spock-core:1.0-groovy-2.4"          // Spock (Mandatory)
}

但是,当我取消注释Kafka依赖项时,构建会失败,如下所示:

$ gradle clean test
:clean
:compileJava UP-TO-DATE
:compileGroovy
:processResources UP-TO-DATE
:classes
:compileTestJava UP-TO-DATE
:compileTestGroovy
:processTestResources UP-TO-DATE
:testClasses
:test

RootControllerTest > LoadPage should return the string root/index FAILED
    java.lang.ExceptionInInitializerError at RootControllerTest.groovy:21
        Caused by: java.lang.IllegalStateException at RootControllerTest.groovy:21

1 test completed, 1 failed
:test FAILED

FAILURE: Build failed with an exception.

导致失败的是什么?

0 个答案:

没有答案