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
相关推荐
东东51626 分钟前
基于vue的电商购物网站vue +ssm
java·前端·javascript·vue.js·毕业设计·毕设
她说..32 分钟前
策略模式+工厂模式实现审批流(面试问答版)
java·后端·spring·面试·springboot·策略模式·javaee
鹿角片ljp33 分钟前
力扣9.回文数-转字符双指针和反转数字
java·数据结构·算法
skywalker_1133 分钟前
网络编程篇
java·网络协议·网络编程
毕设源码-朱学姐44 分钟前
【开题答辩全过程】以 基于Java的九价疫苗预约系统为例,包含答辩的问题和答案
java·开发语言
tb_first1 小时前
SSM速通4
java·jvm·spring·tomcat·maven·mybatis
百炼成神 LV@菜哥1 小时前
Kylin Linux V10 aarch64安装DBeaver
java·linux·服务器·kylin
有代理ip1 小时前
成功请求的密码:HTTP 2 开头响应码深度解析
java·大数据·python·算法·php
好好沉淀2 小时前
ES 脚本核心语法:ctx._source [‘group_id‘]
java·elasticsearch·script
李慕婉学姐2 小时前
【开题答辩过程】以《基于Spring Boot的疗养院理疗管理系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·spring boot·后端