JUnit Testing (with Spring MVC and Hibernate): IllegalArgumentException: Unknown entity

大兔子大兔子 提交于 2020-02-06 02:15:33

问题


I'm tryng to do some JUnit persistance tests. I'm using Spring MVC and Hibernate.

I have my TestConfig file looking like this:

@ComponentScan({"src.main.java.ar.edu.itba.paw.persistence", })
@Configuration
public class TestConfig {

    @Bean
    public DataSource dataSource() {
        final SimpleDriverDataSource ds = new SimpleDriverDataSource();
        ds.setDriverClass(JDBCDriver.class);
        ds.setUrl("jdbc:hsqldb:mem:paw");
        ds.setUsername("ha");
        ds.setPassword("");
        return ds;
    }

     @Bean
     public LocalContainerEntityManagerFactoryBean getEntityManagerFactory() {
         final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
         factoryBean.setPackagesToScan("src.main.java.ar.edu.itba.paw.models");
         factoryBean.setDataSource(dataSource());
         final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
         factoryBean.setJpaVendorAdapter(vendorAdapter);
         final Properties properties = new Properties();
         properties.setProperty("hibernate.hbm2ddl.auto", "update");
         properties.setProperty("hibernate.search.default.directory_provider", "filesystem");
         properties.setProperty("hibernate.search.default.indexBase", "lucene/indexes");
         properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
         properties.setProperty("hibernate.show_sql", "true");
         properties.setProperty("format_sql", "true");
         factoryBean.setJpaProperties(properties);
         return factoryBean;
       }



     @Bean
     public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
          return new JpaTransactionManager(emf);
     }
}

And here's the test I'm trying to run.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class)
@Transactional
public class UserHibernateDaoTest {

    private static final long USERID = 1;
    private static final long NONEXISTENTUSERID = -1;
    private static final String FIRSTNAME = "TestFirstName";
    private static final String LASTNAME = "TestLastName";
    private static final String EMAIL = "test1@mail.com";
    private static final String PASSWORD = "TestPassword";
    private static final String PHONENUMBER = "0000000";
    private static final String ROLE = "USER";

    @PersistenceContext
    private EntityManager em;

    @Autowired 
    private UserHibernateDao userHibernateDao;  
    private JdbcTemplate jdbcTemplate;

    @Before
    @Transactional
    public void setUp() {
        this.userHibernateDao = new UserHibernateDao();
            User u;
            u = new User();
            u.setUserid(123);
            u.setFirstName(FIRSTNAME);
            u.setLastName(LASTNAME);
            u.setEmail(EMAIL);
            u.setPassword(PASSWORD);
            u.setPhoneNumber(PHONENUMBER);
            u.setRole(ROLE);
            em.persist(u);

    }

    @Rollback
    @Test
    public void testCreate() {
    //  just trying to run this empty test
    }
}

The thing is I'm not even able to run that simple empty test because I get a couple of exceptions, the last one being:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ar.edu.itba.paw.persistence.UserHibernateDao] 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)}

Any ideas on how to solve this?


回答1:


When doing test you must give a complete spring config to be able to instantiate your test class.

In your case you have an autowire for the UserHibernateDao class attribute, but noting currently give a way for spring to wire this class. As you are directly instantiating the UserHibernateDao inside your setup method. Just removing the autowire should fix your issue.



来源:https://stackoverflow.com/questions/59754882/junit-testing-with-spring-mvc-and-hibernate-illegalargumentexception-unknown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!