随笔分类 - Groovy
摘要:在之前的博文里曾使用GMaven插件编译Groovy/Spock,这次使用GMavenplus插件,更加方便。 具体步骤 1. 导入Spock和Groovy依赖 2.配置GMavenPlus <plugin> <groupId>org.codehaus.gmavenplus</groupId> <a
阅读全文
摘要:平时经常会用Ant来写一写脚本,但最近跨入到Groovy的时代,试着做一些改变。Groovy里集成了AntBuilder能非常方便的调用到Ant的对象模型。现考察如下1. Groovy里定义的属性如何能在Ant的Task里调用?1 def testMsg = "hello world"2 def a...
阅读全文
摘要:def props = new Properties()new File("foo.properties").withInputStream { s -> props.load(s) }println props
阅读全文
摘要:http://mrhaki.blogspot.com/2014/12/gradle-goodness-continue-build-even.html介绍了不少使用Groovy编写脚本的好例子,可以参考。
阅读全文
摘要:Link:http://arturoherrero.com/2011/06/04/10-groovy-one-liners-to-impress-your-friends/I find that comparing programming languages is a worthwhile exer...
阅读全文
摘要:已经在项目里使用Groovy/Spock做测试框架了,感觉和Maven结合在一起还是挺好用的。在Maven的pom.xml里引入他们还是挺方便的,第一先要在dependency 里引入 org.codehaus.groovy groovy-all 1.8.1 provided org.spockframework spock-core 0.6-groovy-1.8 test...
阅读全文
摘要:闭包实现了 isCase 方法,这样闭包可以在 grep 和 sw itch 中作为分类器使用,在这种情况下,各自的参数传递给闭包,然后调用闭包进行计算得到一个 Boo lean 值(参考 6.1 节),正如你所见:assert [1,2,3].grep{ it<3 } == [1,2]switch(10){case {it%2 == 1} : assert false}这样可以让我们使用任何逻辑进行分类,又一次证明了这种可能性,因为闭包也是对象。
阅读全文
摘要:一般想到Groovy是JVM上的动态语言,都不知道它还有Static Typeing的功能import groovy.transform.TypeChecked void someMethod() {} @TypeCheckedvoid test() { // compilation error: // cannot find matching method sommeeMethod() sommeeMethod() def name = "Marion" // compilation error: // the variable naaammme is...
阅读全文
摘要:如题,,具体介绍请参看:http://refaktor.blogspot.com/2012/07/private-fields-and-methods-are-not.html
阅读全文
摘要:一直有在项目中使用Maven来编译,测试,发布Java代码。 最近一直有在学习Groovy。也有考虑使用Gradle来重写build 脚本,但后来发现了GMaven, 是一个不想大动干戈的情况下不错的选择。我已经说服Team的成员使用GMaven了,相应地我也已经把测试测框架做了小小的微调。Step by Step(修改pom.xml):1. 引入Groovy库<dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId>
阅读全文
摘要:Groovy对Java的语法支持算是做的比较好的了,但还是有些地方不尽相同。最近在项目中想用Groovy 写针对JNI方法的测试。发现Groovy的代码是不认Java 的native关键字。所以还得是中间用Java的Class做一个Proxy才可以。// in Demo.javapublic class Demo{ public native int testmethod(); public int doAction(){ return testmethod(); }}// in JNIDemoTest.groovypublic class JNIDemoTest{ @Testpublic..
阅读全文
摘要:Groovy里的GString是immutable的,测试一下下面的代码输出a = 1quote = "${a}"println quotea = 2println quote上面的两次输出是一样的,都是1, 而不是期望的第一次是1,第二次是2那有什么办法能快速解决的。当然是有的。a = 1quote = "${->a}"println quotea = 2println quote使用Closure就可以搞定这个问题。【Note】Groovy里的Closure很是强大,应好好理解,善加使用,威力无比。
阅读全文
摘要:Spock is a testing and specification framework for Java and Groovy applications. What makes it stand out from the crowd is its beautiful and highly expressive specification language.Link: http://code.google.com/p/spock/sample Maven pom.xml:http://code.google.com/p/spock/wiki/HelloSpockPom可以使用when/th
阅读全文
摘要:Link : http://stackoverflow.com/questions/11580057/using-class-from-groovy-file-located-in-another-folder-gradle If you change your build.gradle to simply be task hello << { def utils = new ...
阅读全文
摘要:今天看一用法可以实现Thread-safe closure in Groovy class Stock { def order Stock(orderObject) { order = orderObject } def shares(closure) { closure = closure.clone() // Ensure thread safity closure.delegate = order closure() order } }
阅读全文
摘要:Groovy里with 关键字的使用 def calendar = Calendar.instancecalendar.with { clear() set MONTH, JULY set DATE, 4 set YEAR, 1776 println time}
阅读全文
摘要:Groovy里的Optional函数可以不像C++那样,一定得放在参数列表的最后,这个是一大改进 class Person{ def age, name Person(n="buhaiqing", a ){ name =n age = a } } def o = new Person(23) ...
阅读全文
摘要:1. 简单的数据驱动测试 [ ["andy", 32], ["haiqing", 32], ] .each { name, age -> println "name is " + name + " age is " + age } 怎么样,简单吧!!
阅读全文
摘要:def test(a,b){ return a+b } 如果你已有个List,又厌倦了一个一个地传参,你可以使用 test(*[2,3]) NOTE:这里的*会自动地把一个List对象进行展开。但注意List里元素的个数,必须和函数参数的个数相等才行。
阅读全文
摘要:inject是一个累积的过程,比方说实现从1到9的累加 def result = (1..9).inject(0){ sum , i -> sum += i } print result
阅读全文

浙公网安备 33010602011771号