To autowire or not to autowire

Since using Spring 2.5, I switched from the XML-based application context to the annotations. Although I find those very useful and huge time savers, I’ve always had the feeling that I was losing something in term of flexibility. In particular the @Autowired annotation – or the standard @Inject – felt to me like the new “new”, increasing the coupling between my classes and making it harder to change implementation when needed.

I still feel that way a bit, but I’ve learned an interesting pattern to limit the problem when it comes to testing my code, i.e. when I want to replace the real implementation of a bean for a mock.

Let’s illustrate with an example. I want to build an application to find interesting stuff on the web for me. I will start with a service which takes a URL and bookmarks it when if it’s a new one which happens to be interesting.

Until recently, I may have coded something like this:

@Named
public class AwesomenessFinder {

  @Inject
  private BlogAnalyzer blogAnalyzer;

  @Inject
  private BookmarkService bookmarkService;

  public void checkBlog(String url) {
    if (!bookmarkService.contains(url) && blogAnalyzer.isInteresting(url)) {
      bookmarkService.bookmark(url);
    }
  }
}

This is bad, can you see why? If not, keep reading, I hope you will learn something useful today.

Because I’m conscientious, I want to create unit tests for this code. Hopefully my algorithm is fine but I want to make sure it won’t bookmark boring blogs or bookmark the same URL twice.

That’s where the problems appear, I want to isolate the AwesomenessFinder from its dependencies. If I was using an XML configuration, I could simply inject a mock implementation in my test context, can I do it with the annotations? Well, yes! There’s a way, with the @Primary annotation. Let’s try creating mock implementations for BlogAnalyzer and BookmarkService.

@Named
@Primary
public class BlogAnalyzerMock implements BlogAnalyzer {

  public boolean isInteresting(String url) {
    return true;
  }
}

@Named
@Primary
public class BookmarkServiceMock implements BookmarkService {

  Set bookmarks = new HashSet();

  public boolean contains(String url) {
    return bookmarks.contains(url);
  }

  public void bookmark(String url) {
    bookmarks.add(url);
  }
}

Because I use Maven and I put those mocks in the test/java directory, the main application won’t see them and will inject the real implementations. On the other hand, the unit tests will see 2 implementations. The @Primary is required to prevent an exception like:

org.springframework.beans.factory.NoSuchBeanDefinitionException:
No unique bean of type [service.BlogAnalyzer] is defined: expected single matching bean
but found 2: [blogAnalyzerMock, blogAnalyzerImpl]

Now, I can test my algorithm:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application-context.xml")
public class AwesomenessFinderTest {

  @Inject
  private AwesomenessFinder awesomenessFinder;

  @Inject
  private BookmarkService bookmarkService;

  @Test
  public void checkInterestingBlog_bookmarked() {
    String url = "http://www.javaspecialists.eu";
    assertFalse(bookmarkService.contains(url));
    awesomenessFinder.checkBlog(url);
    assertTrue(bookmarkService.contains(url));
  }
}

Not bad, I tested the happy path, an interesting blog gets bookmarked. Now how do I go about testing the other cases. Of course I can add some logic in my mocks to find certain URLs already bookmarked or not interesting, but it would become clunky. And this is a very simple algorithm, imagine how bad it would get to test something more complex.

There is a better way which requires to redesign my class and the way the dependencies are injected. Here is how:

@Named
public class AwesomenessFinder {

  private BlogAnalyzer blogAnalyzer;

  private BookmarkService bookmarkService;

  @Inject
  public AwesomenessFinder(BlogAnalyzer blogAnalyzer, BookmarkService bookmarkService) {
    this.blogAnalyzer = blogAnalyzer;
    this.bookmarkService = bookmarkService;
  }

  public void checkBlog(String url) {
    if (!bookmarkService.contains(url) && blogAnalyzer.isInteresting(url)) {
      bookmarkService.bookmark(url);
    }
  }
}

Note that I still autowire my dependencies with the @Inject annotation, so the callers of my AwesomenessFinder won’t be affected. For example, the following in a client class will still work:

@Inject
private AwesomenessFinder awesomenessFinder;

However, the big difference is that I autowire at the constructor level, which gives me a clean way to inject mock implementations. And, since we’re mocking, let’s use a mocking library. Last year, I wrote a post about mockito where I used ugly setters to inject my mocks. With the technique mentioned here, I don’t need to expose my dependencies anymore, I get a much better encapsulation.

Here is what the updated test case looks like:

public class AwesomenessFinderTest {

  @Test
  public void checkInterestingBlog_bookmarked() {
    BookmarkService bookmarkService = mock(BookmarkService.class);
    when(bookmarkService.contains(anyString())).thenReturn(false);

    BlogAnalyzer blogAnalyzer = mock(BlogAnalyzer.class);
    when(blogAnalyzer.isInteresting(anyString())).thenReturn(true);

    AwesomenessFinder awesomenessFinder = new AwesomenessFinder(blogAnalyzer, bookmarkService);

    String url = "http://www.javaspecialists.eu";
    awesomenessFinder.checkBlog(url);

    verify(bookmarkService).bookmark(url);
  }
}

Note that this is now plain java, there is no need to use Spring to inject the mocks. Also, the definition of those mock is in the same place than their usage which eases the maintenance.

To go a step further, let’s implement other test cases. To avoid code duplication we’ll refactor the test class and introduce some enums to keep the test cases as expressive as possible.

public class AwesomenessFinderTest {

  private enum Knowledge {KNOWN, UNKNOWN};

  private enum Quality {INTERESTING, BORING};

  private enum ExpectedBookmark {STORED, IGNORED}

  private enum ExpectedAnalysis {ANALYZED, SKIPPED}

  @Test
  public void checkInterestingBlog_bookmarked() {
    checkCase(Knowledge.UNKNOWN, Quality.INTERESTING,
        ExpectedBookmark.STORED, ExpectedAnalysis.ANALYZED);
  }

  @Test
  public void checkBoringBlog_ignored() {
    checkCase(Knowledge.UNKNOWN, Quality.BORING,
        ExpectedBookmark.IGNORED, ExpectedAnalysis.ANALYZED);
  }

  @Test
  public void checkKnownBlog_ignored() {
    checkCase(Knowledge.KNOWN, Quality.INTERESTING,
        ExpectedBookmark.IGNORED, ExpectedAnalysis.SKIPPED);
  }

  private void checkCase(Knowledge knowledge, Quality quality,
                         ExpectedBookmark expectedBookmark, ExpectedAnalysis expectedAnalysis) {

    BookmarkService bookmarkService = mock(BookmarkService.class);
    boolean alreadyBookmarked = (knowledge == Knowledge.KNOWN) ? true : false;
    when(bookmarkService.contains(anyString())).thenReturn(alreadyBookmarked);

    BlogAnalyzer blogAnalyzer = mock(BlogAnalyzer.class);
    boolean interesting = (quality ==  Quality.INTERESTING) ? true : false;
    when(blogAnalyzer.isInteresting(anyString())).thenReturn(interesting);

    AwesomenessFinder awesomenessFinder = new AwesomenessFinder(blogAnalyzer, bookmarkService);

    String url = "whatever";
    awesomenessFinder.checkBlog(url);

    if (expectedBookmark == ExpectedBookmark.STORED) {
      verify(bookmarkService).bookmark(url);
    } else {
      verify(bookmarkService, never()).bookmark(url);
    }

    if (expectedAnalysis == ExpectedAnalysis.ANALYZED) {
      verify(blogAnalyzer).isInteresting(url);
    } else {
      verify(blogAnalyzer, never()).isInteresting(url);
    }
  }
}

Last but not least, a nice bonus to the injection by constructor is the capacity to have all the dependencies of a class in the same place (the constructor). If the list of dependencies grow beyond control, you get a very obvious code smell with the size of the constructor. It’s a sign that you certainly have more than one responsibility in your class and you should split it into multiple classes, easier to isolate for unit testing.

3 thoughts on “To autowire or not to autowire

  1. This is a well made example of why using private field injection is an anti-pattern: Because it makes your code depend on a dependency injection framework.

  2. Damien, I don’t agree with you. You can use @Inject on private fields and still be able to unit test easily with Mockito.

    For example let’s take a MyClass class with @Injected private fields. In MyClassTest class, I annotate the MyClass field with @InjectMocks, and dependencies with @Mock.
    No need to use ugly setters or whatever, it works : mocked dependencies are injected in MyClass.

    So when in comes to testing code, I think @Autowired/@Inject on private fields are still suitable. What do you think ?

    1. Bonjour Jerome,

      Thanks for the valuable comment, I will need to revisit the question. I wasn’t aware of the mockito annotations and they seem very useful to work around the private field injection. I tried it quickly and it works like a charm. I’ll have to check for JMock which is the one I use at work now. It seems they also have an @Mock annotation but I don’t see any equivalent for @InjectMocks.

      I’m not sure if that’s a good thing if mocking libraries need to make up for this possible anti-pattern though. I’ll certainly give it more thoughts and post a new article to follow up. Thanks again.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>