Field Injection Kills Unit Tests

Spring is loved for Dependency Injection. And who doesn’t love @Autowired, where you can just put this annotation on a variable and Spring intelligently fetches the right bean for you?

However, if you want to perform unit tests on your controllers, using @Autowired is a bad idea. How would you be able to mock your service if it is autowired?

The better practice is to use settings and constructors, and make the variable in constructor final, like this:

public MyController(final MyService svc) { ... }

Now, have fun mocking 🙂

Advertisements

Custom HttpMessageConverter to accept JSON response with wrong content type

I am using a web service that is serves JSONP. The response content type I get from it is “text/javascript” (which is non-standard, I believe it should be “application/javascript” for jsonp). However, the service can also return JSON, and I am using Spring’s RestTemplate to call this service to return JSON.  This is the error I get when I try to convert it into Map[] objects via rest.getForEntity(targetUrl, Map[].class);

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [LMap[].class;] and content type [text/javascript]

Problem:
Web service returning wrong content type, causing RestClientException to be thrown, because Spring does not know how to convert such response into the class you want.

Solution:
Because I know the result will be JSON, I created a custom HttpMessageConverter that behaves the same as MappingJacksonHttpMessageConverter, but supports all content types.

public class MappingAnyJsonHttpMessageConverter
  extends MappingJacksonHttpMessageConverter {

  public MappingAnyJsonHttpMessageConverter() {
    List list = new ArrayList(1);
    list.add(MediaType.ALL);
    this.setSupportedMediaTypes(list);
  }
}

Now, tell my RestTemplate to use this message converter

RestTemplate rest = new RestTemplate();
List<HttpMessageConverter<?>> msgConverters =
  new ArrayList<HttpMessageConverter<?>>(1);
msgConverters.add(new MappingAnyJsonHttpMessageConverter());
rest.setMessageConverters(msgConverters);

ResponseEntity<Map[]> response =
rest.getForEntity(targetUrl, Map[].class);
Map[] jsonResult = response.getBody();

No more errors 🙂

Refreshing on data structures and algorithms

If you are not currently in school or not fresh out of school, chances are you have forgotten some of the less often used data structures and algorithms.

If you would like a refresher, try leetcode.com, where you can do the practice right there and it will judge you and compare your time against other solutions in terms of time. The interface is similar to that I have experienced in a few Coursera courses I’ve taken. You submit your answer via their embedded code editor, and it will test your submission against predefined test cases and let you know the result and the performance (time and sometimes memory).

An all time good read would be Effective Java (old school is cool, still valid). You might event learn things that you’ve never picked up from school and work.

First time prepping food in 2015

image

When was the last time i made food that is not from the freezer? Probably not this year! Well, today is the day…
One IGA trip and 15 minutes later…

Smoked prosciutto + double creme brie + cucumber on toasted french baguette, dressed with strawberry and fig balsamic reduction.

“Long name, amazing result.” as I’ve heard on the radio.

Rock’n Kobe @ Kingyo Izakaya

image

ROCK’N KOBE – $22.00
STONE-GRILLED MARINATED KOBE BEEF WITH 2 KINDS OF KINGYO’S ORIGINAL SAUCE.

I prefer my beef to be more raw… so about 5 seconds on each side for me.

Review: The hot stone makes the dish interesting, but the fun makes it easy to overcook the beef.  Despite some being overcooked, it was still tender. However, the beef didn’t taste like Kobe beef (though I am comparing with the ones I actually had in Steakland, in KOBE, Japan).  So, order it for the fun, not for the Kobe taste.

Click to add a blog post for Kingyo Izakaya on Zomato