java - How to control the @PostConstruct when integration testing using Spring -


i have spring bean class redisrepo inside initialising database connection @postconstruct:

 @postconstruct     public void init() {         logger.debug("redisrepo, init.");         client = new redisclient(redis_host, redis_port);         ...     } 

i creating bean using java config @ springconfiguration.class:

 @bean     @scope("singleton")     public redisrepo redisjrepo() {         return new redisrepo();     } 

i started build integration tests using spring. using same configuration class (springconfiguration.class) tests:

@runwith(springjunit4classrunner.class) @springapplicationconfiguration(classes = springconfiguration.class) 

my test class using embedded-redis need init right before start tests:

 @before     public void init() throws ioexception { //init embedded-redis } 

the problem when start tests @postconstruct of redisrepo class executed before integration-test init() class (past below) leading me null since embedded redis hasnt initialised yet.

how avoid it? mybe not doing right?

thanks, ray.

i suggest consider using spring boot auto-configuration (@enableautoconfiguration or @springbootapplication) initialize redis connection. can use these spring boot properties customize redis:

# redis (redisproperties) spring.redis.database= # database name spring.redis.host=localhost # server host spring.redis.password= # server password spring.redis.port=6379 # connection port spring.redis.pool.max-idle=8 # pool settings ... spring.redis.pool.min-idle=0 spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1 spring.redis.sentinel.master= # name of redis server spring.redis.sentinel.nodes= # comma-separated list of host:port pairs 

this remove need connection initialization in @postconstruct production code , can autowire redis related beans context dusting testing.

edit1:

to populate redis instance before testing method can use @beforemethod (using testng) or @before (using junit). populate before test after context initialized use @postconstruct in test class.

edit2:

you asked on generic rule how overcome need initialization of resources in @postconstruct. believe problem how wiring beans in application.

your @postconstruct initialization done in other bean, redisclient stored variable client. argue mixing of concerns. if register redisclient bean spring context way:

@bean public redisclient redisclient() {     redisclient client = new redisclient(redis_host, redis_port);     ...     return client; } 

you can autowire bean had @postconstruct initialization. able autowire during test. if redisclient not thread-safe, may want consider prototype or request scope it.

with approach, extremely use @postconstruct , use spring ioc container handle reusable resource instances.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -