maven+spock

pom配置

话说Junit+Mockito的组合用起来是真难用,还是Spock的简单,尤其是参数化的测试。junit的Parameter是鸡肋,杂恶心;Theories用来也不爽。

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <packaging>jar</packaging>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tom</groupId>
    <artifactId>lspock</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <groovy.version>2.4.1</groovy.version>
        <surefire.version>3.1.2</surefire.version>
        <spock-core.version>1.0-groovy-2.4</spock-core.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>

            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${surefire.version}</version>
                <configuration>
                    <testSourceDirectory>src/test/java</testSourceDirectory>
                    <useFile>false</useFile>
                    <includes>
                        <include>**/*Spec.java</include> <!-- Yes, .java extension -->
                        <include>**/*Test.java</include> <!-- Just in case of having also "normal" JUnit tests -->
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>${spock-core.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>${groovy.version}</version>
        </dependency>

        <dependency> <!-- only required if Hamcrest matchers are used -->
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>2.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

test类

java 复制代码
package com.tom.lspock.model

import org.hamcrest.MatcherAssert
import org.hamcrest.Matchers
import spock.lang.Specification

class MyModelTest extends Specification {

    def "test_model"() {
        given:
        String s = ""

        when:
        println(1234)
        def replace = s.replace("s", "--")

        then:
        MatcherAssert.assertThat("myStringOfNote", Matchers.startsWith("my"))
        MatcherAssert.assertThat(replace, Matchers.equalTo(""))
    }
}

mvn clean test

shell 复制代码
 ~/work/llixi/lspock/ [master+*] mvn clean test
相关推荐
P.H. Infinity27 分钟前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天31 分钟前
java的threadlocal为何内存泄漏
java
caridle43 分钟前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
^velpro^1 小时前
数据库连接池的创建
java·开发语言·数据库
苹果醋31 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
秋の花1 小时前
【JAVA基础】Java集合基础
java·开发语言·windows
小松学前端1 小时前
第六章 7.0 LinkList
java·开发语言·网络
Wx-bishekaifayuan1 小时前
django电商易购系统-计算机设计毕业源码61059
java·spring boot·spring·spring cloud·django·sqlite·guava
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
全栈开发圈1 小时前
新书速览|Java网络爬虫精解与实践
java·开发语言·爬虫