我尝试遵循https://tarunsapra.wordpress.com/2011/06/28/junit-spring-session-and-request-scope-beans/上的指南 或https://touk.pl/blog/2011/04/15/how-to-test-spring-session-scoped-beans/。 但是,我无法在JUnit4测试中自动连接会话bean。
也许这只是一些愚蠢的错误,但是我找不到它。
我使用spring-test 4.3.22.RELEASE(以及其他各种spring-libraries)和junit-4.12.jar
这是我的琐碎例子(在Eclipse Oxygen.3a Release(4.7.3a)中进行了测试)
TrivialSessionTest.java
package demo;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/trivialApplicationContextForSessions.xml" })
@ComponentScan("demo")
public class TrivialSessionTests {
@Autowired
protected SessionTestBean sessionTestBean;
@Test
public void testLogin() {
Assert.assertEquals("Hello World", sessionTestBean.getSomething());
}
}
SessionTestBean.java
package demo;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
@Component
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class SessionTestBean {
public SessionTestBean() {}
public String getSomething() {
return "Hello World";
}
}
spring / trivialApplicationContextForSessions.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="sessionScopeConfigurer" class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="session">
<bean class="org.springframework.context.support.SimpleThreadScope" />
</entry>
<entry key="request">
<bean class="org.springframework.web.context.request.RequestScope" />
</entry>
</map>
</property>
</bean>
</beans>
运行测试结果
[main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@73e132e0] to prepare test instance [demo.TrivialSessionTests@3773862a]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demo.TrivialSessionTests': Unsatisfied dependency expressed through field 'sessionTestBean'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'demo.SessionTestBean' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
答案 0 :(得分:0)
抱歉,经过进一步的深入试验,我终于找到了问题所在:
因此,运行示例如下
TrivialSessionTests.java
package demo;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
@org.junit.runner.RunWith(org.springframework.test.context.junit4.SpringJUnit4ClassRunner.class)
@org.springframework.test.context.ContextConfiguration(locations = { "classpath:spring/trivialApplicationContextForSessions.xml" })
@org.springframework.test.context.web.WebAppConfiguration
public class TrivialSessionTests {
@Autowired()
protected SessionTestBean sessionTestBean;
@Test
public void testLogin() {
Assert.assertEquals("Hello World", sessionTestBean.getSomething());
}
}
SessionTestBean.java
package demo;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
@Component(value="sessionTestBean")
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class SessionTestBean {
public SessionTestBean() {}
public String getSomething() {
return "Hello World";
}
}
trivialApplicationContextForSessions.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="demo" />
</beans>