Spring 集成 Spock

2016/01/01 Java

Spock 是一个专门用来写测试的框架。

添加依赖

<!-- spock spring tes 集成 begin -->

<!-- Mandatory dependencies for using Spock -->
<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-core</artifactId>
    <version>1.1-groovy-2.4</version>
    <scope>test</scope>
</dependency>
<!-- Optional dependencies for using Spock -->
<dependency> <!-- use a specific Groovy version rather than the one specified by spock-core -->
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>2.4.3</version>
    <scope>test</scope>
</dependency>
<dependency> <!-- enables mocking of classes (in addition to interfaces) -->
    <groupId>cglib</groupId>
    <artifactId>cglib-nodep</artifactId>
    <version>3.1</version>
    <scope>test</scope>
</dependency>
<dependency><!-- enables mocking of classes without default constructor (together with CGLIB) -->
    <groupId>org.objenesis</groupId>
    <artifactId>objenesis</artifactId>
    <version>2.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-spring</artifactId>
    <version>1.1-groovy-2.4</version>
    <scope>test</scope>
</dependency>
<!-- spock spring tes 集成 end -->

添加测试

/**
 * spring boot 测试启动公共注解
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SpringBootTest(classes = Application.class)
@ActiveProfiles
public @interface ApplicationTest {
    @AliasFor(annotation = ActiveProfiles.class, attribute = "profiles") String[] activeProfiles() default {"local"};
}
import spock.lang.Specification

@ApplicationTest
class SpringBeanSpec extends Specification {

    def "Spring bean @Value 注入测试"() {
        when:
        def exportService = SpringContextUtil.getBean(ExportService.class)
        then:
        exportService.getTmpHtmldir() != null
    }
}

Search

    Table of Contents