Spring.NoSuchBeanDefinitionException infering with <pre-post-annotations= "enabled" />?



注意:我解决了这个问题。请参阅自己的答案:)

我有一个单元测试,用于测试弹簧安全性中的不同角色。

所有的身份验证和授权都是通过用户界面实现和工作的,但我似乎无法让它在单元测试中工作。这是我的单元测试:

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

如果我注释掉最后一个测试"userWithReadRoleHasNOAccessToTheMethod",一切正常。这是最后一个测试,不会引发异常访问拒绝异常。这是因为在我的测试上下文中,我需要标签全局方法安全性pre-post-annotations="enabled"。

然而,有趣的是,如果我将此标签添加到我的测试上下文.xml,我会在所有单元测试中得到一种非常不同的错误:

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

它说我没有用id userService定义bean,但是,它以前工作过,而且它显然在我的测试上下文中.xml:

  <bean id="userService" class="com.sysunite.ccy.pms.qms.security.UserService">
    <property name="store" ref="quadStore"/>
    <property name="instanceService" ref="instanceService"/>
  </bean>
  <security:global-method-security pre-post-annotations="enabled"/>

同样,如果我注释掉pre-post-annotations=enabled标签,一切正常,但是我无法测试@preAuthorize注释。有人知道这怎么可能吗?这让我感到困惑。

提前感谢!

好吧,

我花了一段时间,但我终于让它工作了。

基本上我犯了两个主要错误,其中一个是你作为问题的读者看不到的。

在我的用户服务类中,我有以下代码:

  @Override
  @PreAuthorize("hasRole('ADMIN_ROLE')")
  public void deleteUser(String username) {
    // implementation of the method
  }

这导致了某种死锁,导致 Spring 给出 Spring.NoSuchBeanDefinitionExceptionerror。我不确定为什么,但是当 Spring 实例化这个类而标签注释 = 启用时,Spring 实际上不能实例化 userService 类。

我的第二个错误和新的学习点:使用 @PreAuthorize 注释时,请始终确保它所在的类由 spring 上下文实例化。就我而言,我在自己的测试类中拥有该方法,该方法未由 spring 上下文实例化。它确实有@RunWith(SpringJUnit4ClassRunner.class)注释,但显然这不是Spring :)

感谢您的帮助:)

最新更新