Hiberailooving, Part 2: Modeling and Testing
Posted by Joe Rinehart at 12:30 PM
3 comments - Categories:
Hiberailooving
The Hiberailooving series chronicles the construction of a development environment and server configuration for creating redistributable JEE+CFML applications using Groovy, Spring, Hibernate, and Railo.
To get up and running with Hiberailooving, we're going to start with a basic task: creating an entity and unit testing it. For a lack of better examples (I've burned through Contacts and Widgets all too often), we're going to work with Bicycles. My task for this part of the Hiberailoovy series is to create a Bicycle model and unit test it from within Eclipse.
First, I'm going to need to add some Eclipse plug-ins. We're just going to add what we need now - we'll add more later.
- The Web Tools Platform (WTP) - Extends Eclipse to provide editing for Java EE Web applications.
- Groovy Eclipse Plugin - Code editor for Groovy. Go ahead and get the TestNG plug-in now, too.
Next, we'll need the TestNG library to do unit tests. While Groovy has JUnit support, it doesn't "just work" with Eclipse. TestNG does.
- TestNG - Download the latest version (not the Eclipse plug-in) and unzip it.
Allrighty. After some downloading and installing, we're ready to go. As I'm going to be building out a Web application, I need to create a suitable project.
Creating a Dynamic Web Project
We'll use a WTP wizard to create our Web project. From the Java EE perspective, that's File -> New -> Project -> Other -> Web -> Dynamic Web Project. I'll name it "BikeShop". I'm going to leave "Target Runtime" blank - we'll address that later. Click "Finish," and we're good to go.
Adding TestNG
To annotate our tests, we'll need the TestNG .jar. From the TestNG .zip you downloaded, drop testng-5.8-jdk15.jar file into your project's /lib directory. Right click your project and add it to your build path, adding this .jar under "Libraries"
Adding Groovy Support...
...couldn't be simpler. Right-click the project and choose "Add Groovy Nature." I like the sound of that. "Groovy Libraries" should now show up as an entry under the src folder ("Java Resources: src").
Creating our Groovy class and its test.
I'm going to create a class called Bike, giving two properties: id and sold. I'll then add a method called sell() that updates sold to "false." Because I'm a TDD kind of guy, tho, I'll first write the unit test.
First, I'll add a package by right-clicking src and choosing New -> Package. I'll add com.firemoss.bikeshop.test.
Creating the Test
Again, easy as can be. Right click in the package and do "New..." From the Groovy options, selected TestNG test. We'll add a line to import our (to be created) model package, and overwrite the stubbed-in "testSomething" function as follows:
BikeTest.groovy
import com.firemoss.bikeshop.model.*;
import org.testng.annotations.*
import org.testng.TestNG
import org.testng.TestListenerAdapter
import static org.testng.AssertJUnit.*;
public class BikeTest {
public static void main(String[] args){
def testng = new TestNG()
testng.setTestClasses(BikeTest)
testng.addListener(new TestListenerAdapter())
testng.run()
}
@Test
final void testSellBike(){
def bike = new Bike();
assertFalse(bike.sold);
bike.sell();
assertTrue(bike.sold);
}
}
Creating the Model
Here's where we'll see just how nice and terse Groovy can be. No getters, no setters (except those generated at compile-time for Java-friendliness).
We create another package (com.firemoss.bikeshop.model), and again do "New...," this time selecting that we'd like to create a new Groovy class. We'll name it "Bike".
Here's its code:
Bike.groovy
class Bike {
int id
boolean sold = false;
void sell()
{
sold = true;
}
}
Running our Test
We can right-click our test case, choose "Run As...", and run it as a TestNG class. You'll see the TestNG view pop up, and you'll hopefully see it pass its test.
Conclusion and "Next Time"
Next time on Hiberailooving, we'll add a Wheel class (because a bike has-many, but no more than two, Wheels). It'll set up a more complex test (including testing a business rule that'll throw an exception), and create the infamous one-to-many relationship that we'll eventually want Hibernate to persist.
John Allen wrote on 06/25/08 1:46 PM
This stuff is cool.Once again Joe, you make me smarter.
Really looking forward to this.