Showing posts with label Groovy. Show all posts
Showing posts with label Groovy. Show all posts

Use input parameter with mock

@SpringBean
MyClass myClass = Stub {
    method(_) >> {
        Integer id -> map.get(id)
    }
}

-------- or --------

@SpringBean
MyClass myClass = Mock()

given:
myClass.method(_) >> {
        Integer id -> map.get(id)
}

Split a URL into protocol, domain, port and uri using regular expressions

Posted on by Kim

// Split URL into protocol, domain, port and URI
Pattern pattern = ~/(https?:\/\/)([^:^\/]*)(:\d*)?(.*)?/
Matcher matcher = pattern.matcher url
matcher.find()

String protocol = matcher.group 1
String domain = matcher.group 2
String port = matcher.group 3
String uri = matcher.group 4

Encode url (Change spaces etc.)

Posted on by Kim

URL url = new URL(urlStr)
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef())
url = uri.toURL()

Groovy & JSON

Posted on by Kim

import groovy.json.JsonSlurper

class TestJson {

    static String json = "{shortText=1, errorContent=2, messageContent=3, errorCode=4}"
    static main(args) {
        def shortText = 1        def errorContent = 2        def messageContent = 3        def errorCode = 4        def jsonValue = "{" +
                "\"shortText\"     : ${shortText}," +
                "\"errorContent\"  : ${errorContent}," +
                "\"messageContent\": ${messageContent}," +
                "\"errorCode\"     : ${errorCode}" +
                "}"
        def result = new JsonSlurper().parseText jsonValue

        System.out.println "result.shortText..:${result.shortText}"        System.out.println "result.errorContent..:${result.errorContent}"        System.out.println "result.messageContent..:${result.messageContent}"        System.out.println "result.errorCode..:${result.errorCode}"
    }
}

Sample Gradle, Groovy Project on IntelliJ

Posted on by Kim

Change the build.gradle Add mavenLocal and right plugins...

See: The Groovy Plugin

group 'as.moes.gradletest'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'jetty'
apply plugin: 'war'

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.11'

    testCompile group: 'junit', name: 'junit', version: '4.11'
}

Groovy Spock test form Java Maven project

Posted on by Kim

Setup in maven


    
        junit
        junit
        4.12
        test
    
    
        org.spockframework
        spock-core
        1.0-groovy-2.4
        test
    
    
        org.codehaus.groovy
        groovy-all
        2.4.4
        test
    
    
        com.athaydes
        spock-reports
        1.2.7
        test
    



    org.apache.maven.plugins
    maven-compiler-plugin
    
        UTF-8
        1.8
        ${jdk.target.version}
    


    org.codehaus.gmavenplus
    gmavenplus-plugin
    1.5
    
        
            
                addTestSources
                testCompile
            
        
    


    org.apache.maven.plugins
    maven-surefire-plugin
    2.18.1
    
        
            **/*Test.java
            **/*Spec.java
        
    

See : Writing Unit Tests With Spock Framework: Creating a Maven Project Thanks to Petri Kainulainen