用junit测试获取spring bean 报No Scope registered for scope 'session'错
2013-05-20 13:55
603 查看
spring 配置如下
<bean name="i18nManager" class="com.thams.service.I18nManager" scope="session"> <property name="messageSource" ref="messageSource" /> </bean>
测试类
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"file:WebRoot/WEB-INF/spring/*.xml"}) @TestExecutionListeners({ WebContextTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class }) public class testi18n { @Autowired private I18nManager i18nManager; @Test public void test01(){ i18nManager.setLocale(Locale.ENGLISH); System.out.println(i18nManager.getMessage("title"));; } }
注意:需要在测试类头中加入
@TestExecutionListeners({ WebContextTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
是关键.
其中的WebContextTestExecutionListener类需要在类路径中自行创建
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.Scope; import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.SimpleThreadScope; import org.springframework.test.context.TestContext; import org.springframework.test.context.support.AbstractTestExecutionListener; public class WebContextTestExecutionListener extends AbstractTestExecutionListener { @Override public void prepareTestInstance(TestContext testContext) throws Exception { if (testContext.getApplicationContext() instanceof GenericApplicationContext) { GenericApplicationContext context = (GenericApplicationContext) testContext.getApplicationContext(); ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); Scope requestScope = new SimpleThreadScope(); beanFactory.registerScope("request", requestScope); Scope sessionScope = new SimpleThreadScope(); beanFactory.registerScope("session", sessionScope); } } }
ok .这样就可以使用junit 测试 scope 为request 或者 session的bean了
=========================================================================
如果你没有使用spring-test框架
而是通过 XmlWebApplicationContext ClassPathXmlApplicationContext 来获取上下文的话.
可以参考下面:
将下面代码加入到你的获取上下文中
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpSession session = new MockHttpSession(); request.setSession(session); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
会提示少jar.
把spring-mock.jar 加入即可
阅读更多相关文章推荐
- junit 测试问题 No qualifying bean of type [javax.servlet.http.HttpServletRequest] found for dependency
- No setter found for property 'mapperLocation' in class 'org.mybatis.spring.SqlSessionFactoryBean' [c
- junit 测试问题 No qualifying bean of type [javax.servlet.http.HttpServletRequest] found for dependency
- Spring No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSessionReposit
- Attribute "scope" must be declared for element type "bean"-[spring]
- Spring 3 和 Hibernate4 整合时遇到的 No Session found for current thread !
- Spring的JUnit错误:NoSuchBeanDefinitionException: No unique bean of type,expected single matching bean but found 2 [dataSource1,data
- Spring-Hibernate - No Session found for current thread
- junit测试spring整合ActiveMQ编写消费者代码报错:org.springframework.beans.factory.BeanCreationException: Error crea
- Spring3 + Hibernate4 配置 No Session found for current thread问题的解决
- spring3 与 Hibernate4集成报错 No Session found for current thread
- Spring 配置事务管理无效(No Session found for current thread)原因及解决方案
- 【Junit 报错】No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
- 关于在Spring中Hibernate的No Session found for current thread问题
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is d
- Hibernate4 与 spring3 集成Hibernate4 No Session found for current thread
- spring-session整合redis集群的时候,No qualifying bean of type [org.springframework.session.SessionRepository
- org.springframework.beans.factory.BeanCreationException: Scope 'request' is not active for the current thread
- spring 调用 bean (xml 和 注解) sessionFactory no this bean