viernes, junio 08, 2012

Testing Abstract Classes (and Template Method Pattern in Particular)



Sick at heart and lonely, deep in dark despair. Thinking one thought only, where is she tell me where. (Heart Full of Soul - The Yardbirds).

From wikipedia "A template method defines the program skeleton of an algorithm. One or more of the algorithm steps can be overridden by subclasses to allow differing behaviors while ensuring that the overarching algorithm is still followed".

Typically this pattern is composed by two or more classes, one that is an abstract class providing template methods (non-abstract) that have calls to abstract methods implemented by one or more concrete subclasses.

Often template abstract class and concrete implementations reside in the same project, but depending on the scope of the project, these concrete objects will be implemented into another project.

In this post we are going to see how to test template method pattern when concrete classes are implemented on external project, or more general how to test abstract classes.

Let's see a simple example of template method pattern. Consider a class which is responsible of receiving a vector of integers and calculate the Euclidean norm. These integers could be received from multiple sources, and is left to each project to provide a way to obtain them.

The template class looks like:

Now another project could extend previous class and make an implementation of  abstract calculator by providing an implementation of read() method .

Developer that has written a concrete implementation will test only read() method, he can "trust" that developer of abstract class has tested non-abstract methods.

But how are we going to write unit tests over calculate method if class is abstract and an implementation of read() method is required?

The first approach could be creating a fake implementation:

This is not a bad approach, but has some disadvantages:
  • Test will be less readable, readers should know the existence of these fake classes and must know exactly what are they doing. 
  • As a test writer you will spend time in implementing fake classes, in this case it is simple, but your project could have more than one abstract class without implementation, or even with more than one abstract method.
  • Behaviour of fake classes are "hard-coded".
A better way is using Mockito to mock only abstract method meanwhile implementation of non-abstract methods are called.


Mockito simplifies the testing of abstract classes by calling real methods, and only stubbing abstract methods. See that in this case because we are calling real methods by default, instead of using the typical when() then() structure, doReturn schema must be used.

Of course this approach can be only used if your project does not contain a concrete implementation of algorithm or your project will be a part of a 3rd party library on another project. In the other cases the best way of attacking the problem is by testing the implemented class.

Download sourcecode

Music: http://www.youtube.com/watch?v=9O6eGOu27DA

3 comentarios:

Libin Huang dijo...

20160509libinmulberry outlet store
true religion outlet
herve leger dresses
pandora jewelry
cartier watches
polo ralph lauren
cheap oakley sunglasses
tory burch outlet online
nfl jersey wholesale
swarovski jewelry
ray ban sunglasses
michael kors online
michael kors outlet
snapbacks wholesale
lebron james shoes
nike blazer pas cher
michael kors outlet online
mcm backpack
coach outlet
tory burch outlet online
michael kors handbags
calvin klein underwear
kate spade outlet
oakley sunglasses
michael kors outlet
louis vuitton handbags
true religion canada
asics,asics israel,asics shoes,asics running shoes,asics israel,asics gel,asics running,asics gel nimbus,asics gel kayano
gucci outlet online
michael kors outlet
air max 2015
nike air force 1
swarovski crystal
fitflops shoes
timberland shoes

chenlina dijo...

chenlina20160716
coach outlet store online
jordan shoes
christian louboutin shoes
michael kors outlet
cheap jerseys
air jordan 13
cheap jordan shoes
jordan 13
coach outlet clearance
authentic louis vuitton handbags
louis vuitton outlet stores
louis vuitton outlet
nike roshe run
asics outlet
jordan 4 toro
louis vuitton
true religion
ray ban sunglasses outlet
coach outlet store online clearances
nike air max 90
nike outlet store
michael kors handbags
coach outlet
christian louboutin outlet
tory burch outlet online
oakley sunglasses
ralph lauren outlet
gucci outlet
nike air max
kobe 9
louis vuitton handbags
polo ralph lauren outlet
louboutin pas cher
ray ban sunglasses
louis vuitton handbags
true religion jeans
air jordan 8
asics shoes
louis vuitton outlet
nike sb shoes
as

Gege Dai dijo...

oakley sunglasses
cartier sunglasses
police sunglasses
beats headphones
fred perry polo shirts
tods outlet online
michael kors handbags
jordan 11
michael kors uk
ralph lauren polo
coach outlet online
ralph lauren polo
tiffany and co
oakley sunglasses wholesale
tory burch outlet
polo ralph lauren
tory burch outlet
mlb jerseys
cartier watches
michael kors outlet
tiffany outlet
tory burch shoes
oakley sunglasses wholesale
michael kors outlet store
marc jacobs sale
nike roshe run
prada sunglasses for women
longchamp pliage
true religion sale
nike air max 90
ed hardy clothing
polo ralph lauren
ferragamo shoes
longchamp outlet online
michael kors outlet online
czq20160722

Donate If You Can and Find Post Useful