0

I use Spring Security to authentication. I modified my annotation @Page which define which type of user is able to access page.

After that in my acceptance tests i started to get errors in:

public void login(@Named("email") String email, @Named("password") String password) {
    fillUpLoginForm(email, password);
    waitForElement(MosquitoElements.MainNavigationBar.LOGOUT);
    securityService.login(email, password); // authenticates user in test environment too
}

i found that there's something wrong here:

        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(email, password);
    getSecurityContext().setAuthentication(authenticationManager.authenticate(token));

What can cause problem?

Error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.securityContext': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

Change in annotation:

before

boolean requiresAdminPrivileges() default false;

after

Authorities[] requiredAuthorities();

In appliaction everything works fine, only tests have probles.

Rob
  • 4,927
  • 12
  • 49
  • 54
akuzma
  • 1,592
  • 6
  • 22
  • 49
  • See this question http://stackoverflow.com/questions/5136944/spring-test-session-scope-bean-using-junit – Maksym Demidas Mar 25 '13 at 13:03
  • @MaksymDemidas it's not the same problem, tests before changes worked fine too, and I have all mentioned in that question made – akuzma Mar 25 '13 at 14:06
  • It's not the same question but it's the same problem: you need to emulate session in the test enveronment (where by default there is no web session). Try the second solution from mentioned question (SimpleThreadScope). – Maksym Demidas Mar 25 '13 at 14:16
  • @MaksymDemidas I use prototype scopes to testing, they don't have their Scope classes since they are hardcoded into bean factory. – akuzma Mar 25 '13 at 14:21
  • `Scope 'session' is not active for the current thread; ` from posted exception log clearly indicates that you need to turn on session scope support for tests – Maksym Demidas Mar 25 '13 at 14:39

1 Answers1

1

Try to emulate sesseion scope in your test's application context:

<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>
Maksym Demidas
  • 7,707
  • 1
  • 29
  • 36
  • unfortunalty error is still the same, but thank you for help, I'll write here solution when I'll find it – akuzma Mar 25 '13 at 14:57
  • try to execute this code before actual test MockHttpServletRequest request = new MockHttpServletRequest(); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); Looks like in your case session mock is not enough, you need to privide mocked HTTP request too – Maksym Demidas Mar 25 '13 at 15:08