我试图在我的应用程序中准备测试配置。我的一个测试看起来与此类似:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(TestApplication.class)
public class SomeTest {
@Autowired private SomeService service;
@Test
public void getLatestConfigurationForDeviceTest() {
Device config = service.getDevice();
assertThat( config ).isNotNull();
...
}
}
SomeService
引用会话范围服务。 TestApplication已配置:
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {
"com.example.myapp.service",
"com.example.myapp.repository",
"com.example.myapp.listener"
})
@EnableJpaRepositories(basePackages = {
"com.example.myapp.repository"
})
@EntityScan(basePackages = {
"com.example.myapp.domain.entity"
})
应用程序抛出异常:
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.sessionBean': Scope 'session' is not active for the current thread;
...
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request,
在春季启动测试中有一些模拟会话的方法吗?此解决方案AbstractSessionTest 无效。
答案 0 :(得分:0)
由于3.2 Spring支持此类测试,请看一下 test web scoped beans
例11.12。会话范围的bean测试
答案 1 :(得分:-1)
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="session">
<bean class="org.springframework.context.support.SimpleThreadScope"/>
</entry>
</map>
</property>
</bean>
&#13;
来源:http://blog.solidcraft.eu/2011/04/how-to-test-spring-session-scoped-beans.html