Tuesday, March 23, 2010

Nice Mercurial tutorial by Joel Spolsky

Joel Spolsky (of Joel on Software) wrote a very entertaining and thorough tutorial on Mercurial, a distributed version control system (DVCS). The tutorial has a Subversion re-education pre-tutorial for those of us with Subversion brain damage. Well worth a look. Sadly, Joel will no longer be blogging, citing the need to focus on his growing software company. His musings on software development will be missed.

Friday, March 19, 2010

Mockito's @InjectMocks annotation does reflection-based DI

I've been using @InjectMocks heavily lately since it came out in version 1.8.3 of mockito. The javadocs for this annotation state that it uses setter injection to inject its dependencies. Being a lazy developer, I was writing a unit test yesterday at work and forgot to write the setter methods for a couple of dependencies on the SUT that I was testing. Lo and behold, the test passed and all mock verifications were satisfied. Very confused, I went back to some other unit tests and their SUTs and removed the setter methods. In all of my cases, the tests continued to pass. A quick note to the mockito list confirms what I discovered--@InjectMocks actually is using a reflection-based DI scheme, not unlike what Spring does when you annotate collaborator fields in a Spring bean with @Autowired and @Resource. Very cool feature, as this further reduces code noise. Here is the discussion on Google Groups.

Tuesday, March 16, 2010

Cool features in Cucumber, part 1

My Cucumber adventure (via cuke4duke) continues. I have been busy writing features and building out step definitions in Groovy. Groovy absolutely rocks for this sort of thing. I'm doing some more elaborate Cucumber features (at least in my novice head) and came upon this blog posting. Very handy stuff. More related to the Ruby version of Cucumber, but most of the concepts transfer over to cuke4duke.

Saturday, March 13, 2010

PeepCode screencast: Use the Cucumber

I've been interested in acceptance testing tools recently and Cucumber has been at the top of my list. PeepCode recently released a screencast on Cucumber. I've viewing it right now; the screencast is excellent and a nice introduction to Cucumber. I'm thinking of using it on a Grails web app. The screencast is based on a Rails 2 web application, which Cucumber seems to integration seamlessly with. Supposedly Cucumber is technology agnostic and works against any sort of web application.

CoverScout for retrieving iTunes album art

I recently purchased the MacHeist nanoBundle, a collection of Mac OS X applications and utilities. One of the utilities included in the bundle is CoverScout, a utility that will allow you to browse your iTunes library and retrieve hard to find album art. I have a fairly large collection of music and iTunes has been pretty average in its attempts to find album art. CoverScout will retrieve album art from many different sources and allow you to select which image to use. The images are also rated by quality. Very cool utility.

Monday, March 08, 2010

New annotations in mockito 1.8.3

Mockito 1.8.3 was recently released and I got a chance to use it today. There are a couple of new annotations available in this release: @Captor, @Spy, and @InjectMocks. I was able to use @Captor and @InjectMocks today. Even after just a few minutes with these two annotations, I'm sold. Very cool enhancement. I spent some quality time with EasyMock last week, and there's nothing easy with EasyMock. Ugh!! If you're hip to using mock objects in your Java unit testing efforts, you really should look at mockito these days. Details about @Captor and @InjectMocks follows.

@Captor


This annotation will automatically create typed argument captors (org.mockito.ArgumentCaptor<T>) in your unit tests. Argument captors are essential in verifying indirect outputs to your mocked collaborators.


public class Test{
@Captor ArgumentCaptor<Foobar> foobarCaptor;

@Before
public void init(){
MockitoAnnotations.init(this);
}

@Test
public void shouldDoSomethingUseful() {
//...
verify(mock.doStuff(foorbarCaptor.capture()));
Foobar capturedFoobar = foobarCaptor.getValue();
assertEquals("foobar", capturedFoobar.getName());
}
}


@InjectMocks


Automatically injects mocks by type using setter injection. Constructor injection is not currently available, but if you want to provide a patch, the mockito team will gladly consider your contribution. I'm actually more interested in reflection-based injection, similar to what Spring uses when annotating dependency fields using @Autowired. Having your unit tests inject dependencies via reflection would help me avoid the set* methods on the implementations. I may have to play with this a bit.


public class FooBarManagerTests {

@Mock private FooDependency mockFoo;
@Mock private BarDependency mockBar;
@InjectMocks private FooBarManager manager = new FooBarManagerImpl();

@Before
public void initMocks() {
// Initializes all mocks and then injects those mocks into the FooManager instance.
MockitoAnnotations.initMocks(this);
}

@Test
public void shouldDoSomething() {
manager.doSomething();
verify(mockFoo).doSomethingToFoo(any(String.class));
verify(mockBar).doSomethingToBar(any(Integer.class));
}
}