Wednesday, October 21, 2009

Eclipse Scrapbook Pages: A quick way to test you java code snippets.

Scrapbook Pages was the functionality which most of the eclipse users don't know. This is a simple and quick way to test any small code snippets before implementing it.

For example if you want to test how you can get year, month and date from calendar class. Then we have to write a test class with proper class name and main method in it… But it is an annoying process to just to test this small code. So in Scrap page you can just paste the code in the files as below and you can execute (Ctrl + U).

Sample Code:

int year = 2009;

int month = 0; // January

int date = 1;

java.util.Calendar cal = java.util.Calendar.getInstance();


cal.set(java.util.Calendar.YEAR, year);

cal.set(java.util.Calendar.MONTH, month);

cal.set(java.util.Calendar.DATE, date);

java.sql.Date sqlDate = new java.sql.Date(cal.getTimeInMillis());



You also select only the lines which you want to test, for example if I want to test only the last line "System.out.println("Bye"); Just select the line and execute …. You only see "Bye" in you console…

This is a Quick way to test your code Snipped and another important thing is you don't have any import statements and class name. So If you want to refer Calendar class you have to refer it with full package structure like this java.util.Calendar. By default it only finds the classes which are in java.util (as normally java does).


IBBoard said...

"if you want to test how you can get year, month and date from calendar class. Then we have to write a test class with proper class name and main method in it… But it is an annoying process to just to test this small code."

It is annoying until someone else wants to understand it as well, at which point Uncle Bob's recommendations of "understanding the API through test classes" and Test Driven Development (or Test Driven Experimentation in this case, I guess) are a great help.

I did it recently with Jetty and Servlets. I only wanted to know how a few bits of creating contexts and adding servlets worked, but by creating a small test suite I a) recorded the findings for other members of my team and b) got to keep track of whether any changes in behaviour were introduced as we update versions of Jetty.

Mohammed Yousuff said...

IBBoard, what i understand is you mean to say to keep this code snipped as a (JUnit test)test classes..

This code is a not complete method and the intension is to see how the values are printed in the console...

I do understand the TDD development, where we have to keep the test cases for every units (or methods), so that whenwever we are making any changes it will show of...

Bottem line is if you want to test some code which is to make sure what the output try using scrappage.....

IBBoard said...

Testing output could be a use for the scrapbook, but so is a unit test that calls toString() on a variety of known objects. The former is a one-shot operation that no-one will know about in future while the latter is a permanent record in case anyone else wants to know what the toString() (or other methods) of an object do. For it to be a useful unit test you'd do an Assert.equals() rather than the println()s. Your unit test in this case would be for java.sql.Date and could test other things (like parsing and formatting, and how it handles leap years, etc).

I actually read an Uncle Bob post recently where he said that one of the first things he does to try to understand how old code that he hasn't worked on recently functions is to read the unit tests as they map out the expected behaviour.

Bottom line is that if you want to test some code to see what it outputs then you're generally testing the toString() method and can keep a record of what you expect it to be using a unit test ;)

Post a Comment


This content comes from a hidden element on this page.

The inline option preserves bound JavaScript events and changes, and it puts the content back where it came from when it is closed.
Click me, it will be preserved!

If you try to open a new ColorBox while it is already open, it will update itself with the new content.

Updating Content Example:
Click here to load new content