首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring.NoSuchBeanDefinitionException与<预后注释=“已启用”/>的整数?

Spring.NoSuchBeanDefinitionException与<预后注释=“已启用”/>的整数?
EN

Stack Overflow用户
提问于 2013-11-06 12:09:40
回答 1查看 527关注 0票数 0

注:我修正了这个问题。见自己的答案:)

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

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

代码语言:javascript
复制
@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',一切正常。这是最后一个不抛出异常AccessDeniedException的测试。这是因为我需要测试上下文中的全局方法安全前注释=“已启用”标记。

但是,这里有趣的是,如果我将这个标记添加到我的test-context.xml中,那么在所有单元测试中都会出现一种完全不同的错误:

代码语言:javascript
复制
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,但是它以前是有效的,它显然在我的test-context.xml中:

代码语言:javascript
复制
  <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"/>

同样,如果我注释掉了预post注释=enabled标记,那么一切都正常,但是我无法测试@preAuthorize注释。有人知道这怎么可能吗?这让我很困惑。

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-06 14:23:39

好吧,这花了我一段时间,但我终于让它起作用了。

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

在我的UserService类中,我有以下代码:

代码语言:javascript
复制
  @Override
  @PreAuthorize("hasRole('ADMIN_ROLE')")
  public void deleteUser(String username) {
    // implementation of the method
  }

这导致了某种死锁,导致Spring给了Spring.NoSuchBeanDefinitionExceptionerror。我不知道为什么,但是当Spring实例化这个类时,当标记注释= enabled存在时,Spring实际上不能实例化userService类。

我的第二个错误和新的学习点是:当使用@PreAuthorize注释时,始终确保它所在的类被spring上下文实例化。在我的例子中,我在我自己的测试类中有一个方法,它没有被spring上下文实例化。它确实有@RunWith(SpringJUnit4ClassRunner.class)注释,但这显然不是针对Spring :)

谢谢你的帮助:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19811763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档