mlinhard / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Provide nice test instance #324

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
When writing unit-tests one often does the following:
* instantiate the class-under-test
* mock away references to other objects
* mock away global objects

Why not provide a nice test-instance which provides the following:
* a spy to the class-under-test
* all primitive attributes set to their 1-values (e.g. int 1, float 1.0, char ' 
', boolean true)
* all references referencing to mocks

Original issue reported on code.google.com by sebidiet...@gmail.com on 5 Mar 2012 at 7:24

GoogleCodeExporter commented 9 years ago
Hi

I don't think this is a good idea. Here is why:
> * a spy to the class-under-test
Spying the tested subject is weird, it doesn't make sense, but for partial 
mocks which is most of the time a bad practice.

> * all primitive attributes set to their 1-values (e.g. int 1, float 1.0, char 
' ', boolean true)
Initializing attribute to 1-value is wrong, we are bypassing the integrity of 
the code under test on attributes whose usage is unknown.

> * all references referencing to mocks
If you need to have so many mock references, something might be wrong in your 
design, check your 'Ce' (efferent coupling) metric in sonar. Besides with the 
included short hand injection utility, that can instantiate the tested type 
some conditions. Check the @InjectMocks javadoc there : 
http://docs.mockito.googlecode.com/hg/latest/org/mockito/InjectMocks.html

However you might be interested by this project, which goes a bit further in 
order to avoid knowledge on collaborators: http://code.google.com/p/komarro/

Original comment by brice.du...@gmail.com on 5 Mar 2012 at 10:35

GoogleCodeExporter commented 9 years ago

Original comment by brice.du...@gmail.com on 3 Sep 2012 at 10:00