java - Injected constructors, unit testing and best practices -


this esoteric questions how public make constructors when using ioc container. i'm using java, guice , junit4, i'm sure question more general.

under best practices in guice docs, says "keep constructors hidden possible". quite agree with, happy go ahead make constructors private , rely on guice instantiation.

however, brings issue mocking classes , unit testing. if constructors private, how can instantiate in unit test , pass in mocked dependency? having create new guice module every unit test seems overkill me. surely, must therefore make constructors public.

which brings me question: given di useful when combined unit testing, best practices in guice docs keeping constructors hidden mistake?.

read further in same document, suggests giving constructors default access. put unit tests in same package.

default access provided if none of public, protected or private specified.

from document referenced:

as correction, limit visibility of both implementation classes, , constructors. typically package private preferred both, facilitates:

  • binding class within module in same package
  • unit testing class through means of direct instantiation

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -