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
相关推荐
DokiDoki之父21 分钟前
Web核心—JSP入门/EL/JSTL标签/MVC+三层架构/一文速通
java·开发语言
寒月霜华22 分钟前
java-高级技术(单元测试、反射)
java·开发语言·单元测试·反射
独自破碎E24 分钟前
Leetcode2166-设计位集
java·数据结构·算法
Cikiss36 分钟前
LeetCode160.相交链表【最通俗易懂版双指针】
java·数据结构·算法·链表
聪明的笨猪猪1 小时前
Java Redis “Sentinel(哨兵)与集群”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
222you2 小时前
Mybatis(1)
java·tomcat·mybatis
靠近彗星2 小时前
1.5操作系统引导
java·linux·服务器·操作系统
瑶山2 小时前
社区版Idea怎么创建Spring Boot项目?Selected Java version 17 is not supported. 问题解决
java·spring boot·intellij-idea·创建项目
学习编程的Kitty2 小时前
JavaEE初阶——多线程(1)初识线程与创建线程
java·开发语言·java-ee
长安城没有风3 小时前
从入门到精通【Redis】初识Redis哨兵机制(Sentinel)
java·数据库·redis·后端