0

I'm trying to prepare tests configuration in my application. One of my tests looks similar to this:

@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 referring to session scope service. TestApplication is configured:

@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"
})

Applications threw an exception:

...
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, 

Is some way to mock session in spring boot tests? This solution AbstractSessionTest doesn't work.

Community
  • 1
  • 1
kris14an
  • 741
  • 7
  • 24

2 Answers2

0

Since 3.2 Spring supports such tests, have a look at test web scoped beans

Example 11.12. Session-scoped bean test

-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>

source: http://blog.solidcraft.eu/2011/04/how-to-test-spring-session-scoped-beans.html

victortav
  • 9
  • 1