Posts Tagged ‘testng’

Five random lessons learned about Scala

Posted in java, scala, software development on April 15th, 2009 by Joerg – 7 Comments

Since a week I am playing around with Scala to see what all this fuss is about. After Twitter has anounced that they are using Scala (see this presentation) there is a real hype now. So far I must admit I am impressed. This language is really nice. I will certainly have a very detailed look. The book “Programming in Scala” is already on its way from Amazon.

The following lessons learned are just a kind of note to self. These are some random experiences I made when playing around. Your first lessons will probably be completely different.

1. Scala != Groovy
Although this is obvious, when you have prior experience in Groovy it is tempting to fall into your Groovy habits. Both languages look similar at a glance but are very different in detail. For instance I very often write def when defining a variable where it should be val or var. The fact that def is a valid keyword in Scala and is used to define methods is not helpful in this regard. There are so many other subtile differences that I can’t mention them here. Just be aware of that.

2. Use a decompiler
A decompiler can be very useful when learning Scala. Just compile the Scala class and then decompile it. This way you can easily see, what the Scala compiler is creating when using certain language features. I discovered a nice decompiler while doing this. It can be found here. The decompiler was key for solving the next issue.

3. Be careful with field level annotations
It is a great feature of Scala that you can use established Java frameworks and just replace the Java code with Scala. This way you can easily use Spring and the likes. I did that but had sudden problems with the Spring @Autowired annotation. I annotated the field like that:

var pollService: IPollService = _;

Spring was starting to complain about missing parameters on the annotated method. What?
Ok, I was aware that Scala generates corresponding methods for a field just like Groovy does. But what was the issue here? The Spring annotation is defined as field level and method level annotation. But when it is used on a method it has to be a setter which gets at least one parameter. The decompiler saved the day. It shows that Scala is applying the annotation three times. At the field, at the setter pollService_$eq() and the getter pollService(). The last causes the Spring exception. (For all Scala experts, the @BeanProperty annotation does not solve the problem)
The (ugly) workaround is to create the setter manually and annotate only this. Here is how:

var pollService: IPollService = _;

def setPollService(pollService: IPollService) = {this.pollService = pollService}

4. Switch of “Compile Scala files first” in IDEA

There is a small bug/inconvinience in the current Scala plugin of IntelliJ Idea. It occurs when you have a project with combined Java and Scala sources. Any Java class that is located in a test source folder will not compile. You will get an error message “ClassX is already defined as class ClassX”. To fix this you can simply change the Scala compiler settings. Go to Settings->Compiler->Scala and uncheck “Compile Scala Files First”. It is not perfect, as you might need to manually (Ctrl-Shift-F9) compile Scala sources that Java files depend on. But at least you can compile everything now.

5. Use Scalatest!
If you want to write tests in Scala try Scalatest. It shows perfectly what can be done in Scala. It enables different styles of testing like xUnit or BDD just by applying different traits. And it enables you to use TestNG in your Scala tests. This way you can just start tests in Scala as if they where Java, at least in Intellij Idea. Here is a small example how a Test can look like:

@Test def testCreateTopicPoll() = {
  val pollDto = preparePoll
  val pollId = pollService saveOrUpdateTopicPoll pollDto;
  pollId should not be null

Look at the last line. Isn’t that beautiful? This is real code. You would not need any comments to explain, what is under test here.

I can strongly recommend that you have a look into Scala. It is a very interesting language with some nice concepts. I hope that some of my lessons learned help you when doing your first steps. Then let me know what lessons you learned!

Inheritance of class annotations in TestNG

Posted in java, software development, testing on April 5th, 2009 by Joerg – 1 Comment

Recently Oliver Fischer introduced TestNG at the Berlin-Brandenburg JUG. At one point there was an interesting discussion with the audience about class level annotations. Why should they be used? You can annotate a class like this:

public class Foo 

Now every public method in this class is considered a test method. Several people in the audience mentioned that they would still prefer to mark the test explicitly. This is just more readable. But if so, what might be a good reason to use class level annotations?

One very nice characteristic of class level annotations is that they can be inherited. Let’s assume you want several classes in a specific testgroup. What you don’t want to do is to annotate each and every test method with @Test(groups = {“MyTestGroup”}). This is simply not DRY. Using inheritance, you can instead create a base-class like this:

@Test(groups = {"MyTestGroup"})
public class BaseClass 

Now each test class that should be in this group can just extend BaseClass.

public class MyTestClass extends BaseClass 

Every public method in these classes is a test method and they belong to the group MyTestGroup. You probably still want to add the @Test annotation to each of the methods for readability. The group assignment is still valid.

What other ideas do you have for making use of annotation inheritance in TestNG?