Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@RefreshScope - JUnit testcases

I am trying to test my controller using JUNIT test cases but finding the below exception

Caused by: java.lang.IllegalStateException: No Scope registered for scope name 'refresh'
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:337)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.getTarget(CglibAopProxy.java:705)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
like image 801
Krishnaveni CH Avatar asked May 18 '18 04:05

Krishnaveni CH


People also ask

How to check JUnit test case results?

1 The Console shows as below where a message reads as ‘This is the test case in this class’. 2 The JUnit result tab displays mainly the number of test cases run, number of errors and number of failures encountered i.e. ... 3 The time taken to finish the execution of the tests. 4 Displays a green bar if all the test cases are passed. More items...

How do I refresh a configuration using @refreshscope?

Putting @RefreshScope on the configuration will indeed refresh the configuration BUT not the beans it created. You need to explicitly define which beans you want to have refreshed. This can be done by either annotating the desired class to be @RefreshScope or the @Bean method to have @RefreshScope.

How do you refresh the scope of a class?

You need to explicitly define which beans you want to have refreshed. This can be done by either annotating the desired class to be @RefreshScope or the @Bean method to have @RefreshScope. Your understanding is a bit off and all of that is stated in the documentation. From the javadoc of @RefreshScope.

How does the refresh scope Bean work?

From the javadoc of @RefreshScope. The implementation involves creating a proxy for every bean in the scope, So you will get 2 instances of the bean. 1 Proxy which will actually wrap an full instance of the bean. When being refreshed, the proxy will survice and the actual instance will be replaced.


1 Answers

try to put in your test file:

@ImportAutoConfiguration(RefreshAutoConfiguration.class)
like image 143
Isma9 Avatar answered Sep 21 '22 23:09

Isma9