1

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 = "[email protected]";
    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
  • Spring is not aware about a bean what should be injected to UserHibernateDao userHibernateDao. Commented Jan 15, 2020 at 15:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you that fixed the exception. Now I'm getting another issue, I just updated the question.
You can look at that question for the new part: stackoverflow.com/questions/19333955/…
By looking at that it just says I have to add @Rollback at my test class or am I getting it wrong?
Well it's a different question than this one, but if you dont want the rollback it would be @Rollback(false).
Ok I'm gonna make another question for that 'cause I'm having some issues. Thanks though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.