Eclipse and jUnit
April 18, 2007 · 16 Comments
I'm doing some consulting right now that has me writing Java code. The more Java I write, the more I'm reminded of why I like ColdFusion so much but that's another story. The company I'm consulting for likes unit testing, which makes me very happy. They actually require that a unit test exists for every method in every class (and in every CFC - yes, they build stuff with ColdFusion as well).
Surprisingly, this is my first exposure to jUnit. Yeah, I know, I'm such a big fan of cfcUnit, how can I never have used jUnit? Anyway, I write my TestFoo.java class with its setUp() and tearDown() methods and start writing testMyMethod() methods. Then it occurs to me: how do I run this? I'm so used to the browser-based runner for cfcUnit that I'd never thought about the Java equivalent.
Out of curiosity, I right-clicked in the editor panel displaying the test case and there, in the Run As... submenu: JUnit Test. Wow! Really? Could it be that simple? Yup! Up pops a JUnit view in the bottom panel running my tests and showing a red failure bar and list of failed tests. Perfect!
Well, of course my tests failed - I just wrote the tests. Next I'll write the code.
As is so often the case, Eclipse continues to surprise me in pleasant ways! Now, where's the cfcUnit plugin?
Tags: cfeclipse · programming

16 responses so far ↓
1 Maxim Porges // Apr 18, 2007 at 8:22 PM
Not sure how new you are to Eclipse with Java, but please tell me you've played with the refactoring and code-gen features already?
Of course, I'll be boring you to death with all this in my talk at cf.objective()... :)
- max
2 Sean Corfield // Apr 18, 2007 at 11:18 PM
I started using Together back in '97 I think and introduced it to Macromedia in '99 or '00. It's always had a variety of great refactorings as well as software metrics and coding standards compliance checks.
I'm looking forward to your talk at cf.Objective()!
3 Brandon Harper // Apr 19, 2007 at 12:54 AM
I would think about giving NetBeans a try if I wasn't already soured by all of the Sun employee blogs chest-thumping about how cool it was about 50 times per day. Come to think of it, I guess that's largely a similar reason BlueDragon isn't on my radar either.
4 Mark Drew // Apr 19, 2007 at 1:22 AM
I shall post more on this when I get my head round cfcUnit...
5 Jim Priest // Apr 19, 2007 at 6:09 AM
6 Qasim Rasheed // Apr 19, 2007 at 8:04 AM
7 Sean Corfield // Apr 19, 2007 at 9:26 AM
@Qasim, it's Command+1 on a Mac (and for folks who didn't know, it's a local rename - that updates all references to renamed item).
8 Qasim Rasheed // Apr 19, 2007 at 2:34 PM
Whenever I have used Ctrl+1, its been more than just local rename. Let's suppose we have a class name Person with no argument constructor. Now in the calling code, if we try to create an instance by using Person p = new Person('CFDeveloper'), the built in complier will complain and Eclipse will place a red mark. Now if you click Ctrl+1 on the error area, Eclipse will present you with an option to create a constructor in your Person class with a string argument.
Is that what you mean when you said local rename?
Thanks for the tip though.
9 Sean Corfield // Apr 19, 2007 at 2:43 PM
10 Chris Scott // Apr 19, 2007 at 5:34 PM
11 Maxim Porges // Apr 19, 2007 at 5:55 PM
Looking forward to seeing you there next month! I've never used Together, but I'm sure the refactoring support is just as good as that in Eclipse. I got exposed to this sort of refactoring tooling in Eclipse first, so it's my first love. Of course, since Eclipse also rules the world of non-.NET IDEs and plug-ins, I love having all my tools in one box: Flex Builder 2, Rational Software Modeler, Subclipse, the Spring IDE, a Tomcat runner/debugger for round-trip debugging Flex and JSP apps, and (of course) CFEclipse.
Since we're having a free-for-all on Eclipse features, I'll give a shout-out to some of my favorites.
Command-Option-Z ("Surround With...") is a biggee; lots of options including try-catch, for/do/if/while, synchronized, and runnable). Quick Fix (Command-1) is great for a lot of different things depending on the context of the red line your cursor is in when it's invoked.
As for code generation, I love Command-Option-S, which does a ton of stuff - everything from various comment blocking, to my favorites: Override/Implement Methods, Generate Getters and Setters (must define private class-level variables first), and hashcode()/equals() generators.
While all the refactoring is awesome, my favorites/commonly used ones are the moves, extractions, and pull up/push downs. I usually like to design stuff before I get coding, but when I want to knock together a quick POC or get an idea out of my head, it can get messy. These refactorings make it so easy to clean things up in a matter of minutes once the solution is complete.
Finally, since I'm a keyboard Nazi, I have a few time savers for running methods and test cases: Command-Option-W pulls up the "Show In..." dialog, and then you can arrow to "Package Explorer" and hit return. If you do this in a method, it will actually select the method in "Package Explorer" for you. Then, if you invoke Option-Shift-X (and wait...) you get a little pop-up menu in the bottom right for what you want to run. Simply hit "T" to run your Test Case, or "J" to run a Java app. I love it.
- max
12 kola // Apr 23, 2007 at 8:51 AM
13 Sean Corfield // Apr 23, 2007 at 11:24 AM
14 Sean Corfield // Apr 23, 2007 at 11:25 AM
http://coolskool.blog-city.com/
15 Andrew Scott // Apr 24, 2007 at 5:07 AM
CFCUnit & CFunit are great examples of getting there, but when you look at tools like maven and a few others it makes builds and Life Cycles great for management.
I would really love to see junit clone for CF, in that I can view the fail/sucess in the same manner as junit.
And refactoring, what another story that is in the java world that would be great for us.
16 Sean Corfield // Apr 24, 2007 at 8:13 AM
Furthermore, cfcUnit (and CFUnit) can be run from ant, again showing fail/success information, as well as from a browser. cfcUnit also has a great Flex front end, courtesy of those folks at asfusion.com.
My previous team used cfcUnit and the ant integration in Eclipse to automatically run unit tests whenever you changed a file. We also used CruiseControl to automatically create and deploy builds.
There are plenty of tools for automating test / build lifecycles with ColdFusion.
Leave a Comment