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.