Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring.NoSuchBeanDefinitionException intefering with <pre-post-annotations="enabled"/>?
    text
    copied!<p>NOTE: I fixed this question. See own answer :)</p> <p>I have a unit test for testing different roles within spring security.</p> <p>All the authentication and authorization is implemented and works through the user interface, but I can't seem to get it working in the unit test. This is my unit test:</p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/test-context.xml"}) @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) public class UserServiceTest { @Autowired UserService userService; @Test public void UserNameAdminMustBeFound(){ UserDetails userDetails = userService.loadUserByUsername("admin"); assertEquals(userDetails.getUsername(), "admin"); } @Test (expected = UsernameNotFoundException.class) public void UserNotFoundMustThrowException(){ userService.loadUserByUsername("NotExistingUser"); } @Test public void userWithAdminRoleHasAccessToTheMethod(){ // admin has ADMIN_ROLE UserDetails userDetails = userService.loadUserByUsername ("admin"); assertEquals("admin", userDetails.getPassword()); this.dummyMethod(); } @Test(expected = AccessDeniedException.class) public void userWithReadRoleHasNOAccessToTheMethod(){ // viewer has VIEWER_ROLE (and thus no ADMIN_ROLE) UserDetails userDetails = userService.loadUserByUsername ("viewer"); assertEquals("viewer", userDetails.getPassword()); this.dummyMethod(); } @PreAuthorize("hasRole('ADMIN_ROLE')") private void dummyMethod(){ System.out.println("dummyMethod reached"); } } </code></pre> <p>If I comment out the last test 'userWithReadRoleHasNOAccessToTheMethod', everything works just fine. It is this last test that doesn't throw an exception AccessDeniedException. This is because I need the tag global-method-security pre-post-annotations="enabled" in my test-context. </p> <p>However, and here it gets interesting, if I add this tag to my test-context.xml, I get a very different kind of error on ALL unit tests: </p> <pre><code>Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@43244fd9] to prepare test instance [com.sysunite.qms.services.UserServiceTest@4f651ff] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.sysunite.qms.services.UserServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.sysunite.ccy.pms.qms.security.UserService com.sysunite.qms.services.UserServiceTest.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sysunite.ccy.pms.qms.security.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.sysunite.ccy.pms.qms.security.UserService com.sysunite.qms.services.UserServiceTest.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sysunite.ccy.pms.qms.security.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sysunite.ccy.pms.qms.security.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} </code></pre> <p>It says I have not defined bean with id userService, however, it worked before, and it is clearly in my test-context.xml:</p> <pre><code> &lt;bean id="userService" class="com.sysunite.ccy.pms.qms.security.UserService"&gt; &lt;property name="store" ref="quadStore"/&gt; &lt;property name="instanceService" ref="instanceService"/&gt; &lt;/bean&gt; &lt;security:global-method-security pre-post-annotations="enabled"/&gt; </code></pre> <p>Again, if i comment out the pre-post-annotations=enabled tag, everything works fine, but then I can't test the @preAuthorize annotation. Anyone has any idea how this is possible? It confuses me.</p> <p>Thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload