maven+spock

pom配置

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



<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>jarpackaging>
    <modelVersion>4.0.0modelVersion>

    <groupId>com.tomgroupId>
    <artifactId>lspockartifactId>
    <version>1.0-SNAPSHOTversion>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <groovy.version>2.4.1groovy.version>
        <surefire.version>3.1.2surefire.version>
        <spock-core.version>1.0-groovy-2.4spock-core.version>
    properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmavenplusgroupId>
                <artifactId>gmavenplus-pluginartifactId>
                <version>1.4version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compilegoal>
                            <goal>testCompilegoal>
                        goals>
                    execution>
                executions>

            plugin>
            <plugin>
                <artifactId>maven-surefire-pluginartifactId>
                <version>${surefire.version}version>
                <configuration>
                    <testSourceDirectory>src/test/javatestSourceDirectory>
                    <useFile>falseuseFile>
                    <includes>
                        <include>**/*Spec.javainclude> 
                        <include>**/*Test.javainclude> 
                    includes>
                configuration>
            plugin>
        plugins>
    build>
    <dependencies>
        <dependency>
            <groupId>org.spockframeworkgroupId>
            <artifactId>spock-coreartifactId>
            <version>${spock-core.version}version>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.codehaus.groovygroupId>
            <artifactId>groovy-allartifactId>
            <version>${groovy.version}version>
        dependency>

        <dependency> 
            <groupId>org.hamcrestgroupId>
            <artifactId>hamcrest-coreartifactId>
            <version>2.2version>
            <scope>testscope>
        dependency>
    dependencies>
project>

test类

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

 ~/work/llixi/lspock/ [master+*] mvn clean test

maven+spock_第1张图片

你可能感兴趣的:(jave,web,maven,java)