Monday, February 4, 2013

Declaring Instances as Fields in Interfaces is a Testability Problem

Typically, when you see fields defined in Java interfaces, they are things you would normally think of as "constants" -- e.g. int's, Strings, maybe an enum or two. I had never really thought much about the fact that you can have instances of more complex classes as interface fields until I actually saw it in practice. Something like this:

public interface MyInterface {
  // Singleton instance.
  MyInterfaceImpl INSTANCE = new MyInterfaceImpl();
  // Interface methods...
  void doSomething();

Then you can use that field like a static variable, most likely as an old-school (pre-dependency-injection) singleton instance.

There are two problems with this as far as testability is concerned. First, you have the usual difficulty you get with static instances in unit tests, namely, that you cannot insert a mock, stub, or fake in all of the places where it is referenced. But if that is not enough to convince you to not use this pattern (hint: it should be), there is another problem that is arguably even more insidious.

Let's say that by some stroke of luck, you are trying to test a class that does not make static reference to the singleton instance and accepts the dependency properly. Now, when you go to create a mock or fake implementation of MyInterface for use in the test, Java still has to instantiate MyInterfaceImpl at classload-time. Depending on what all that involves, this may be expensive, unstable, or flat-out fail in the test environment, thereby thwarting your efforts to properly unit-test.

Obviously, the easiest way to solve this problem is to avoid it in the first place. But if you happen to run into it in a legacy system, you can start migrating away from it gradually by simply moving the instance to another class, or by using something like the value-holder pattern or initialization-on-demand holder idiom that moves the creation of the instance out of the classload sequence for the interface.


jaffa said...

It is very good content and useful information. I love this blog page. I read this content is very interesting and truthful info. So I am remember your blog to my further doubts. Thanks for sharing good article in your blog page.

social marketplace

Jessi Gianni said...

Great post, thanks for providing this information.

we buy junk cars orlando

Mohita Gupta said...

Looking for a software company needs an assessment of the experience levels and expertise that they have in the domain of software development.A reliable and reputable software development company in Jaipur is to interact with the clients from time to time and every change.

jowdjbrown said...

There’s quite a bit of truth to what Mark is saying about startup software developers, but my guess is that there’s another reason that it seems harder to find developers in this context.outsourcing developers in Ukraine

jowdjbrown said...

I had never really thought much about the fact that you can have instances of more complex classes as interface fields until I actually saw it in practice. System Mechanic pro coupon

Nhà Đất Xinh said...

Nếu bạn đang muốn đăng tin bán nhà hay bán đất hoặc bạn muốn mua nhà hay đất thì hãy đến với chúng tôi , với chất lương hàng đầu chúng tôi sẽ giúp các bạn , đăng tin và xem các khu vực nha dat go vap, nha dat quan 9, nha dat thu duc, nha dat binh tan, nha dat tan phu, nha dat tan binh và các khu vực khác trên toàn quốc với uy tín và hiệu quả cao khi bạn đến với chúng tôi.

Michael Syaukas said...

Nice Post, Thanks for your very useful information

Michael Syaukas said...

Canon PIXMA PRO-10S driver Windows & Mac
Canon MG2570 Free Driver Download
Canon iP7270 Free Driver Download
HP Deskjet Ink Advantage 1515 Driver Download
HP Deskjet Ink Advantage 2545 Driver Download
Download Driver HP PSC 1410
Canon MG6340 Driver Mac Linux Windows
Download HP DeskJet F370 Driver
Download Canon MP280 Driver
Download Canon Pixma MG7120 Driver
Download Canon PIXMA MG5640 Driver
Free Download Flash Player (IE)
Free Download Flash Player (Non-IE)
Canon imageCLASS MF8050Cn Driver
HP LaserJet Pro M706n Driver
UC Browser Latest Version 2015 for Windows
Free Download SlimBrowser 2015
Epson Artisan 730 Printer Driver
Epson Expression Home XP-310 Printer Driver
Epson Expression Home XP-510 Printer Driver

Akash Chauhan said...

Software development is an expanding and diverse field. I greatly admire anyone who is able to learn and design software! I've been taking a few classes, but I'm not very proficient in Python or C++. Maybe I'll learn more someday!

software development company in jaipur

Robert F. Crocker said...

It is very helpful for me , I really appreciate thanks for sharing. I would like to read more information thanks.

amine lahragui said...

I appreciate you and hopping for some more informative posts. happy new year 2016 new year 2016 happy new year 2016 images feliz año nuevo 2016 imagenes de año nuevo 2016 feliz año 2016 happy new year images 2016 new year 2016 images new year images 2016 bonne année 2016