Showing posts with label groovy. Show all posts
Showing posts with label groovy. Show all posts

Thursday, May 3, 2007

Unit Testing with Groovy




So we know what the Groovy is and how to eat it. Now it is time to do something useful in terms of methodology. I'm agilist and the thing that I do after knowing constructors and reductors is how to test it. Again Groovy is really helpful, and easy to use. So, you don't need to add additional libraries or do some hacking to use tests in Groovy - they are provided in a default installation. By the default installation i mean get a Groovy from local ftp untar it and there it is ready to be used. So here is a simple example:

Create a class Square.groovy

class Square{
def square(x) {x*x}
}


Create a class SquareTest.groovy

import Square

class SquareTest extends GroovyTestCase{

void testSquare(){
Square square = new Square()
assertEquals( square.square(3),9)
}
}


be sure that both classes are in one catalog. In a command line just type:

groovy SquareTest.groovy


and see the result - everything is fine. It is the easiest xUnit framework that I've seen.
One thing about classes in Groovy - they are public by default, same as methods.

Now let's say that we want to create a framework in maven or we already have one and we want to add some Groovy classes or Groovy test classes. This task is more complicated - because of maven, it is a good tool but sometimes when you search in a xml files for a bug it it terrible, I hate it, but again it is like 200 times better then ant.

so to create a maven project (I'm using maven 2.*) just type:

mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app


Now goto my-app and edit pom.xml add:

<dependency>
<groupid>groovy</groupid>
<artifactid>groovy-all</artifactid>
<version>1.0</version>
<scope>test</scope>
</dependency>


create a folder groovy in my-app/src/main directory and in the my-app/src/test. In other words just in this same place as java folder. And put a *.groovy class there. Go back to the my-app directory, and run:

mvn groovy:compile


to compile main classes.

mvn groovy:testCompile


to compile test classes.

You might ask "Cool, but is there a way to compile classes just by calling normal mvn install or mvn compile?". Yes there is, just add this code to your pom.xml file:


<build>
<plugins>
<plugin>
<groupid>org.codehaus.mojo</groupid>
<artifactid>groovy-maven-plugin</artifactid>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>


Now every time you compile or run tests also groovy classes are going to be included.

Wednesday, April 25, 2007

Old world of Java





I've been coding in Java language for some time now. To be honest I many times complained about features that it just doesn't have. It's really easy to pick some things that are missing there. Hey, but it is going to be better. Recently I've been reading Neal's blog and as I can clearly see it is going to happen. The world of Java is going to receive plenty of goodies in a new Dolphin release, but why wait. Groovy, Nice, SISC-scheme of course are already there. They are not fully featured but they do have "the meat". I don't have to say that sic-scheme is my favorite one, but it is also the most scary one for the typical Java developer so lets begin with something easier - Groovy.

Groovy workspace

This framework is really easy to use and it has a pretty good documentation. So it has plugins for Eclipse, Idea, Emacs, JDeveloper and plenty other editors. Be aware that it is really hard (and I do mean it) to create a good plugin for a highly dynamic language (take a look at those for Ruby, thats what I call fun) so be prepared that sometimes they have hard time telling you what attributes or methods some object has. Right, First cool thing is literals for lists and maps (no need to construct a class). Finally I can create a list just by typing:

By the string "->" I mean "returns".


foo = [1, 3, 4, "sigma", "lambda"]
foo[1] -> 3
foo[3] -> "sigma"


And yes lists are polymorphic just as dictionaries called maps here.


bar = [123:4, "gamma":"omega", "pi":3.14]

bar["gamma"] -> "omega"
bar[123] -> 4


or in a shorter way

bar.gamma -> "omega"

unfortunately when you try to execute

bar.123 an error will accure


This syntaxt is much more modern and in my opinion much better then typical construction with "new" prefix. Im also glad that dynamic and static typing is supported - hey, maybe one day I will be able to do all the tricks that I can do with VB.NET language (now don't laugh it's sad but it is true), but as for now Groovy is not quite yet there.

Next thing that I really missed from python was the string agility, basicly I could use " or ' character as a string and If I wanted to create some complicated string I could just put it into """ block. And here it is ready to use in groovy.


name = "Basho Matsuo"

someXML = """
Fallen sick on a journey,
In dreams I run wildly
Over a withered moor.
${name}
"""



Next sexy thing to investigate is a support for closers - Ladies and Gentlemens finally, Java stops to look like Logo language and introduce some meat that makes your job easier.