我正在尝试使用JUNIT测试用例测试我的控制器,但发现以下异常
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)
at com.pepsico.eip.dao.DAOFactory$$EnhancerBySpringCGLIB$$d1aa4340.getCustomerDAOByDataSource(<generated>)
at com.pepsico.eip.controllers.CustomerController.create(CustomerController.java:147)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
... 80 more
我的TestCase方法是:
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ContextConfiguration(classes= {CustomerDataServiceApplication.class})
@WebMvcTest(CustomerController.class)
@RunWith(SpringRunner.class)
public class CustomerControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testCreateCustomer()
{
String customer="{\"CustID\":4," +
"\"CustName\":\"Raj\"}";
try {
this.mockMvc.perform(post("add/customer").header("Authorization", "Basic FNMVMyQ0VEZWlwOkVJUC" ).
content(customer).
contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("error in test class-testCreateCustomerEntry: "+e.getMessage());
e.printStackTrace();
}
}
我有这个要求从cloud-config服务器获取变量并使用@RefreshScope,如下所示
@RefreshScope
@Component
public @Data class ConfigProperty {
@Value("${dataSource}")
public String dataSource;
public String getDataSource() {
return dataSource;
}
}
另一个我将从上面的课程中获取价值的课程
@RefreshScope
@Component("baseFactory")
public class DAOFactory implements BaseFactory {
@Autowired
private Environment config;
@Autowired
ConfigProperty configProperty;
private static final Logger LOGGER = LoggerFactory.getLogger(DAOFactory.class);
@Autowired
@Qualifier("hazelcastCustomerImpl")
CustomerDAO hazelcastCustomerImpl;
@Autowired
@Qualifier("cassandraCustomerImpl")
CustomerDAO cassandraCustomerImpl;
public CustomerDAO getCustomerDAOByDataSource() {
LOGGER.info("DataSource: " + configProperty.getDataSource());
switch (configProperty.getDataSource().toUpperCase()
)
{
case "CASSANDRA": {
return cassandraCustomerDAOImpl;
}
case "HAZELCAST": {
return hazelcastCustomerDAOImpl;
}
default: {
return cassandraCustomerDAOImpl;
}
}
}
}
请求您帮我解决此问题,谢谢
答案 0 :(得分:1)
尝试放入您的测试文件:
@ImportAutoConfiguration(RefreshAutoConfiguration.class)
答案 1 :(得分:0)
在使用@WebMvcTest(用于测试用例)和@RefreshScope(用于配置)时,我遇到了相同的问题
解决方案:使用
@SpringBootTest
代替
@WebMvcTest
原因:RefreshScope需要完整的容器才能工作,而当我们使用WebMvcTest时,它不会使用完整的容器。 WebMvcTest不会加载spring的所有配置。可能因为这个原因,未加载RefreshScope。