如何解决IllegalStateException:在JUnit测试中找不到用户?

时间:2016-08-05 08:54:10

标签: java spring junit

用于测试我想创建一个用户对象并将其保存以供以后参考:

    User user = userService.createUserInformation("testl", "testpw", "peter", "tester", "test@test.de", "en");

在其中,有一个保存用户的电话:

    public User createUserInformation(...)
       User newUser = new User();
       Authority authority = authorityRepository.findOne("ROLE_USER");
       ...
       newUser.setAuthorities(authorities);
       userRepository.save(newUser);
     }

但是当我尝试这个时,我得到了一个:

org.springframework.dao.InvalidDataAccessApiUsageException: User not found!; nested exception is java.lang.IllegalStateException: User not found!
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:381)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:227)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:131)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
    at com.sun.proxy.$Proxy166.save(Unknown Source)
    at app.backend.service.UserService.createUserInformation(UserService.java:111)
    at 

这意味着什么(找不到用户,而我想保存它?)如何查看User对象的更多细节,或者如何解决此问题?

测试配置基于AbstractControllerTest:

  @WebAppConfiguration
  @ContextConfiguration  
  @ActiveProfiles({"dev", "test"})
  public class AbstractControllerTest extends AbstractTest {

(不知道在哪里可以找到测试的确切应用配置)

非常感谢你的帮助!

2 个答案:

答案 0 :(得分:0)

在您的测试环境中,hibernate无法找到实体或连接到您的数据库。

您可以分享测试类的配置吗?

答案 1 :(得分:0)

问题是只允许登录用户创建新用户。所以,我用以下方式模拟登录用户:

        PowerMockito.mockStatic(SecurityUtils.class);
        Set<GrantedAuthority> authorities = new HashSet<>();

        CustomUserDetails userDetails = new CustomUserDetails(new Long(1), "test12", "testpw", authorities, true, true, true, true);
        PowerMockito.when(SecurityUtils.getCurrentUser()).thenReturn(userDetails);

        User user = userService.createUserInformation("testl", "testpw", "peter", "tester", "test@test.de", "en");

感觉不理想,但它现在有效。 (更好的可能是在种子数据库中有一些测试用户,但不知道如何做到这一点)