Spring Boot测试因缺少javax.servlet.http.HttpServletRequest而失败

时间:2018-02-07 03:38:53

标签: spring-boot spock

我有一个使用Spock进行集成测试的springboot 1.5.9。这些测试之前都使用过SpringBoot 1.3.5,但在升级Spring和Spock之后,我运行时得到以下内容

  

gradle clean build test

每次集成测试时出错:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.servlet.http.HttpServletRequest' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)

我的依赖项:

buildscript {
    ext {
        springBootVersion = '1.5.9.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
dependencies {
    querydslapt group: 'com.mysema.querydsl', name: 'querydsl-jpa', version: '2.8.0', classifier: 'apt-one-jar', transitive: false

compile("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion")
compile("org.springframework.security:spring-security-web:4.0.0.M1")
compile("org.springframework.security:spring-security-config:4.0.0.M1")
compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:2.1.2.RELEASE')
compile('com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.5.0')

compile("org.hibernate:hibernate-core:4.3.4.Final")
compile("org.hibernate:hibernate-entitymanager:4.3.4.Final")
compile("org.hibernate:hibernate-validator")
compile("org.apache.velocity:velocity:1.7")
compile('javax.mail:mail:1.4.1')
compile("org.springframework:spring-context-support")

compile("mysql:mysql-connector-java:5.1.6")
compile("com.h2database:h2:1.3.172")
compile("joda-time:joda-time:2.3")
compile("org.codehaus.groovy.modules.http-builder:http-builder:0.7.1")
compile('org.jadira.usertype:usertype.jodatime:2.0.1')
compile("org.liquibase:liquibase-core")
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.4'
testCompile(group: 'org.spockframework', name: 'spock-core', version: '1.1-groovy-2.4') {
        exclude group: 'org.codehaus.groovy', module: 'groovy-all'
    }

testCompile(group: 'org.spockframework', name: 'spock-spring', version: '1.1-groovy-2.4') {
        exclude group: 'org.spockframework', module: 'spock-core'
        exclude group: 'org.spockframework', module: 'spring-beans'
        exclude group: 'org.spockframework', module: 'spring-test'
        exclude group: 'org.codehaus.groovy', module: 'groovy-all'
    }
testCompile("org.springframework.boot:spring-boot-starter-test:$springBootVersion")
testCompile('org.codehaus.groovy.modules.http-builder:http-builder:0.7+')
testCompile("junit:junit")
}

我所有的spock测试都以此为基础:

@ContextConfiguration(classes = MainClass)
@WebAppConfiguration()
@SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.NONE)
public class BaseSpecification extends Specification {

}

以下是主要课程:

@ComponentScan
@EnableAutoConfiguration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class MainClass {

public static void main(String[] args) {
    ApplicationContext mc = SpringApplication.run( MainClass.class, args );
}

}

1 个答案:

答案 0 :(得分:0)

对于基于Spring的测试,我们需要SpringJUnit4ClassRunner,因此创建了应用程序上下文。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MainClass.class)
@WebAppConfiguration  
@SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.NONE)
public class BaseSpecification extends Specification {

}

并添加

compile("org.springframework.boot:spring-boot-starter-tomcat")